Skip to content

Commit c8b7f47

Browse files
committed
Add documentation for factory registration to readme
1 parent 9944d0e commit c8b7f47

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

README.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ namespace My.Awesome.App
3535

3636
### Register instances
3737

38-
You can register instances to an unity container using `UnityContainerAttributeRegistration.Attribute.RegisterInstanceProviderAttribute` and `UnityContainerAttributeRegistration.Attribute.RegisterInstanceAttribute`.
38+
You can register instances to an unity container using `UnityContainerAttributeRegistration.Attribute.RegisterProviderAttribute` and `UnityContainerAttributeRegistration.Attribute.RegisterInstanceAttribute`.
3939

40-
Classes marked with `UnityContainerAttributeRegistration.Attribute.RegisterInstanceProviderAttribute` will be instantiated using the container which should be populated with the instances.
40+
Classes marked with `UnityContainerAttributeRegistration.Attribute.RegisterProviderAttribute` will be instantiated using the container which should be populated with the instances.
4141
So you can use already registered services to create the instances, which should be later registered.
4242

4343
```
@@ -52,7 +52,7 @@ namespace My.Awesome.App
5252
}
5353
}
5454
55-
[RegisterInstanceProvider]
55+
[RegisterProvider]
5656
public class InstanceProvider
5757
{
5858
[RegisterInstance]
@@ -61,6 +61,45 @@ namespace My.Awesome.App
6161
}
6262
```
6363

64+
### Register Factory
65+
66+
You can register factory methods to an unity container using `UnityContainerAttributeRegistration.Attribute.RegisterProviderAttribute` and `UnityContainerAttributeRegistration.Attribute.RegisterFactoryAttribute`.
67+
68+
Classes marked with `UnityContainerAttributeRegistration.Attribute.RegisterProviderAttribute` will be instantiated using the container which should be populated with the instances.
69+
So you can use already registered services to create the instances, which should be later registered.
70+
71+
Its only important to have the right parameters (see example).
72+
73+
```
74+
namespace My.Awesome.App
75+
{
76+
public class Program
77+
{
78+
public static void Main(string[] args)
79+
{
80+
UnityContainerPopulator populator = new UnityContainerPopulator();
81+
IUnityContainer container = populator.Populate();
82+
}
83+
}
84+
85+
[RegisterProvider]
86+
public class InstanceProvider
87+
{
88+
[RegisterFactory]
89+
public MyClass Factory(IUnityContainer container)
90+
{
91+
// do some magic
92+
}
93+
94+
[RegisterFactory]
95+
public MyClass Factory(IUnityContainer container, Type typeValue, string stringvalue)
96+
{
97+
// do some magic
98+
}
99+
}
100+
}
101+
```
102+
64103
### Using a custom container
65104

66105
It is possible to populate a already created container.

0 commit comments

Comments
 (0)