-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
CreatingAndDisposingContainer.cs
125 lines (99 loc) · 3.11 KB
/
CreatingAndDisposingContainer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*md
<!--Auto-generated from .cs file, the edits made to .md file will be lost! -->
# Creating and Disposing Container
- [Creating and Disposing Container](#creating-and-disposing-container)
- [Creating Container](#creating-container)
- [Rules](#rules)
- [ScopeContext](#scopecontext)
- [Disposing Container](#disposing-container)
## Creating Container
In most cases all you need is:
```cs md*/
namespace DryIoc.Docs;
using System;
using DryIoc;
using NUnit.Framework;
// ReSharper disable UnusedVariable
class Creating_container
{
[Test] public void Example()
{
var container = new Container();
// start using the container..
}
} /*md
```
Actually, the above example is equivalent to:
```cs md*/
public class Creating_container_with_explicit_defaults
{
[Test] public void Example()
{
var container = new Container(rules: Rules.Default, scopeContext: null);
// start using the container..
}
} /*md
```
As you see, it is possible to additionally specify the `rules` and
the [`scopeContext`](ReuseAndScopes.md#what-scopecontext-is).
### Rules
Rules will define the container behavior and conventions.
Rules are immutable, you can create the new rules from the old ones via `With..` methods and
get the rules without some setting via the `Without..` methods.
```cs md*/
public class Adding_some_rules
{
[Test] public void Example()
{
var container1 = new Container(
Rules.Default.With(FactoryMethod.ConstructorWithResolvableArguments));
var container2 = new Container(
Rules.Default.WithoutThrowIfDependencyHasShorterReuseLifespan());
}
} /*md
```
There is an alternate way to specify the rules with existing container rules as a starting point (without explicit mention of `Rules.Default`):
```cs md*/
public class Adding_some_rules_with_action
{
[Test]
public void Example()
{
var container1 = new Container(rules => rules.WithDefaultReuse(Reuse.Singleton));
var container2 = new Container(rules => rules.WithAutoConcreteTypeResolution());
}
} /*md
```
### ScopeContext
ScopeContext is an __optional__ ambient context for the new open scopes to be bound to.
It is described in details [here](ReuseAndScopes.md#scopecontext).
## Disposing Container
`Container` class implements `IDisposable` interface and should be disposed when no longer required.
Disposing container will:
- Dispose resolved Singletons.
- Remove all registrations.
- Set Rules to `Rules.Empty`.
Usage of disposed Container will end-up with exception.
```cs md*/
public class Disposing_container
{
public class MyService : IDisposable
{
public bool IsDisposed { get; private set; }
public void Dispose() => IsDisposed = true;
}
[Test]
public void Example()
{
MyService myService;
using (var container = new Container())
{
container.Register<MyService>(Reuse.Singleton); // MyService is registered with singleton lifetime
myService = container.Resolve<MyService>();
}
Assert.IsTrue(myService.IsDisposed);
}
}
/*md
```
md*/