parent
9aa4d879ca
commit
6e2adc3ece
4 changed files with 66 additions and 0 deletions
@ -0,0 +1 @@ |
|||||||
|
The `FromAssembly` class is a static helper class that supplies methods to find the wanted `Assembly` for an `IAssemblyInstaller`. Is acts similar to a factory in the way that its methods return a new instance of an `IAssemblyInstaller` with the assembly that they provide. |
||||||
@ -1 +1,64 @@ |
|||||||
The `IIocInstaller` interface is the base class for all installers that are used by the `IocContainer`. |
The `IIocInstaller` interface is the base class for all installers that are used by the `IocContainer`. |
||||||
|
|
||||||
|
## Base interface |
||||||
|
|
||||||
|
The base interface only consists of one method, the `Install()` method: |
||||||
|
|
||||||
|
```c# |
||||||
|
void Install(IIocContainer container); |
||||||
|
``` |
||||||
|
|
||||||
|
The `Install()` method installs the given `IRegistrationBase`s in the given `IIocContainer`. |
||||||
|
|
||||||
|
To create your own installer, you have to implement the `IIocInstaller` interface and the `Install()` method. Inside the `Install()` method you have to register the `IRegistrationBase`s with the given `IIocContainer`: |
||||||
|
|
||||||
|
```c# |
||||||
|
container.Register(RegistrationFactory.Register<IFoo, Foo>()); |
||||||
|
``` |
||||||
|
|
||||||
|
The following is an example `IIocInstaller`: |
||||||
|
|
||||||
|
```c# |
||||||
|
public class Installer : IIocInstaller |
||||||
|
{ |
||||||
|
public void Install(IIocContainer container) |
||||||
|
{ |
||||||
|
container.Register(RegistrationFactory.Register<IFoo, Foo>()); |
||||||
|
} |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
## `AssemblyInstaller` |
||||||
|
|
||||||
|
The `AssemblyInstaller` is an implementation of the `IIocInstaller`. It installs all `IIocInstaller`s for a given `Assembly`. |
||||||
|
To do so, it checks each `Type` in the `Assembly` it is given if it is derived from `IIocInstaller`: |
||||||
|
|
||||||
|
```c# |
||||||
|
public AssemblyInstaller(Assembly assembly) |
||||||
|
{ |
||||||
|
Installers = new List<IIocInstaller>(); |
||||||
|
|
||||||
|
Type[] types = assembly.GetTypes(); |
||||||
|
foreach (Type type in types) |
||||||
|
{ |
||||||
|
if (!typeof(IIocInstaller).IsAssignableFrom(type)) |
||||||
|
continue; |
||||||
|
|
||||||
|
Installers.Add((IIovInstaller) Activator.CreateInstance(type));; |
||||||
|
} |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
This list of `IIocInstaller`s is then installed when the `AssemblyInstaller`s `Install()` method is called: |
||||||
|
|
||||||
|
```c# |
||||||
|
public void Install(IIocContainer container) |
||||||
|
{ |
||||||
|
foreach (IIocInstaller installer in Installers) |
||||||
|
{ |
||||||
|
installer.Install(container); |
||||||
|
} |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
With the help of the [`FromAssembly`](FromAssembly) class, `AssemblyInstaller`s can be created and provided with their needed `Assembly`. |
||||||
|
|||||||
Loading…
Reference in new issue