From 05707297ac828c194ab1f30ae18bfbc32baeaa57 Mon Sep 17 00:00:00 2001 From: Simon Gockner Date: Mon, 8 Jul 2019 16:52:39 +0200 Subject: [PATCH] - add documentation of abstract factories --- Abstract-factories.md | 44 +++++++++++++++++++ ...nced-Usage-of-Lightweight-IOC-Container.md | 15 +++++++ Home.md | 1 + _Sidebar.md | 1 + 4 files changed, 61 insertions(+) create mode 100644 Abstract-factories.md diff --git a/Abstract-factories.md b/Abstract-factories.md new file mode 100644 index 0000000..99a434a --- /dev/null +++ b/Abstract-factories.md @@ -0,0 +1,44 @@ +Abstract factories are interfaces, that are implemented by the `LightweightIocContainer`. Using these factories is the recommended way to resolve any instances from the `IocContainer`. + +```c# +public interface IFooFactory +{ + // This is the main structure of every abstract factory +} +``` + +## `Create()` methods + +Every abstract factory has to have a `Create()` method with the return value you want to resolve: + +```c# +IFoo Create(); +``` + +If the constructor of the type you want to resolve has any arguments that can't be resolved by the `IocContainer`, you'll have to pass them as arguments to your `Create()` method: + +```c# +IFoo Create(string name); +``` + +Internally the `Create()` methods call `IocContainer.Resolve<>()`: + +```c# +IFoo foo = container.Resolve(name); +``` + +## `ClearMultitonInstance<>()` methods + +There is the possibility to add a method to the abstract factory that allows to clear `Multiton` instances. This method has to be a generic method to specify the type that is registered as a `Multiton` and whose instances should be deleted: + +```c# +void ClearMultitonInstance(); +``` + +This method for example would clear all created `Multiton` instances of type `IBar`. + +Internally the `ClearMultitonInstance<>()` methods call `IocContainer.ClearMultitonInstances<>()`: + +```c# +container.ClearMultitonInstances(); +``` diff --git a/Advanced-Usage-of-Lightweight-IOC-Container.md b/Advanced-Usage-of-Lightweight-IOC-Container.md index 2ff416d..b5b7db2 100644 --- a/Advanced-Usage-of-Lightweight-IOC-Container.md +++ b/Advanced-Usage-of-Lightweight-IOC-Container.md @@ -33,3 +33,18 @@ public class Installer : IIocInstaller } } ``` + +## Use factories to resolve instances + +Instead of using the `Resolve<>()` method to resolve instances from the `IocContainer` and thus needing an `IocContainer`-object, it is recommended to use [abstract factories](Abstract-factories) to resolve instances. + +Example abstract factory: + +```c# +public interface IFooFactory +{ + IFoo Create(); +} +``` + +These factories are only created as interfaces and will be implemented by the `LightweightIocContainer`. diff --git a/Home.md b/Home.md index 4610db7..185b26b 100644 --- a/Home.md +++ b/Home.md @@ -16,5 +16,6 @@ Welcome to the Lightweight IOC Container wiki! - [Bootstrapping container](Advanced-Usage-of-Lightweight-IOC-Container#Bootstrapping-container) - [Register types with `IIocInstaller`s](Advanced-Usage-of-Lightweight-IOC-Container#Register-types-with-IIocInstallers) - Detailed documentation + - [Abstract factories](Abstract-factories) - [Lifestyles](Lifestyles) - [`RegistrationFactory`](RegistrationFactory) diff --git a/_Sidebar.md b/_Sidebar.md index 8639b6d..76af199 100644 --- a/_Sidebar.md +++ b/_Sidebar.md @@ -14,5 +14,6 @@ - [Bootstrapping container](Advanced-Usage-of-Lightweight-IOC-Container#Bootstrapping-container) - [Register types with `IIocInstaller`s](Advanced-Usage-of-Lightweight-IOC-Container#Register-types-with-IIocInstallers) - Detailed documentation + - [Abstract factories](Abstract-factories) - [Lifestyles](Lifestyles) - [`RegistrationFactory`](RegistrationFactory)