- add documentation of abstract factories

master
Simon Gockner 7 years ago
parent 4a693f08d1
commit 05707297ac
  1. 44
      Abstract-factories.md
  2. 15
      Advanced-Usage-of-Lightweight-IOC-Container.md
  3. 1
      Home.md
  4. 1
      _Sidebar.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<IFoo>(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<IBar>();
```
This method for example would clear all created `Multiton` instances of type `IBar`.
Internally the `ClearMultitonInstance<>()` methods call `IocContainer.ClearMultitonInstances<>()`:
```c#
container.ClearMultitonInstances<IBar>();
```

@ -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`.

@ -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)

@ -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)

Loading…
Cancel
Save