Skip to content

Commit 6a4726e

Browse files
committed
Update readme
1 parent 534a16c commit 6a4726e

1 file changed

Lines changed: 78 additions & 1 deletion

File tree

README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This way you can immediate see if a type is registered or not. Additionally clea
88

99
## Usage
1010

11+
### Register types
12+
1113
You can register types to an unity container using `UnityContainerAttributeRegistration.RegisterTypeAttribute`.
1214

1315
```cs
@@ -19,7 +21,8 @@ namespace My.Awesome.App
1921
{
2022
public static void Main(string[] args)
2123
{
22-
IUnityContainer container = UnityContainerPopulator.Populate();
24+
UnityContainerPopulator populator = new UnityContainerPopulator();
25+
IUnityContainer container = populator.Populate();
2326
}
2427
}
2528

@@ -29,3 +32,77 @@ namespace My.Awesome.App
2932
}
3033
}
3134
```
35+
36+
### Register instances
37+
38+
You can register instances to an unity container using `UnityContainerAttributeRegistration.Attribute.RegisterInstanceProviderAttribute` and `UnityContainerAttributeRegistration.Attribute.RegisterInstanceAttribute`.
39+
40+
Classes marked with `UnityContainerAttributeRegistration.Attribute.RegisterInstanceProviderAttribute` will be instantiated using the container which should be populated with the instances.
41+
So you can use already registered services to create the instances, which should be later registered.
42+
43+
```
44+
namespace My.Awesome.App
45+
{
46+
public class Program
47+
{
48+
public static void Main(string[] args)
49+
{
50+
UnityContainerPopulator populator = new UnityContainerPopulator();
51+
IUnityContainer container = populator.Populate();
52+
}
53+
}
54+
55+
[RegisterInstanceProvider]
56+
public class InstanceProvider
57+
{
58+
[RegisterInstance]
59+
public string Token = "Hard coded token";
60+
}
61+
}
62+
```
63+
64+
### Using a custom container
65+
66+
It is possible to populate a already created container.
67+
68+
```
69+
namespace My.Awesome.App
70+
{
71+
public class Program
72+
{
73+
public static void Main(string[] args)
74+
{
75+
IUnityContainer container = new UnityContainer();
76+
UnityContainerPopulator populator = new UnityContainerPopulator();
77+
populator.Populate(container);
78+
}
79+
}
80+
}
81+
```
82+
83+
### Restrict the checked assemblies
84+
85+
You can restrict the assemblies to check, e. g. to create a container only containing registrations of one assembly.
86+
87+
```
88+
namespace My.Awesome.App
89+
{
90+
public class Program
91+
{
92+
public static void Main(string[] args)
93+
{
94+
IAssemblyProvider assemblyProvider = new CustomAssemblyProvider();
95+
UnityContainerPopulator populator = new UnityContainerPopulator(assemblyProvider);
96+
IUnityContainer container = populator.Populate();
97+
}
98+
}
99+
100+
public class CustomAssemblyProvider : IAssemblyProvider
101+
{
102+
public IList<Assembly> GetAssemblies()
103+
{
104+
// return a list of assemblies to use
105+
}
106+
}
107+
}
108+
```

0 commit comments

Comments
 (0)