Skip to content

Commit cbcd672

Browse files
author
maximv
committed
added the example from the #269 tot the docs
1 parent d6ac824 commit cbcd672

File tree

2 files changed

+117
-16
lines changed

2 files changed

+117
-16
lines changed

docs/DryIoc.Docs/KindsOfChildContainer.cs

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
- [Without Singletons](#without-singletons)
1313
- [With registrations copy](#with-registrations-copy)
1414
- [With no more registration allowed](#with-no-more-registration-allowed)
15+
- [Scenarios from the actual Users](#scenarios-from-the-actual-users)
16+
- [The child container for each test disposed at end of the test without disposing the parent](#the-child-container-for-each-test-disposed-at-end-of-the-test-without-disposing-the-parent)
1517
1618
1719
## No child containers
@@ -68,8 +70,7 @@ public Client(IService service)
6870
}
6971
}
7072

71-
[Test]
72-
public void Facade_for_tests()
73+
[Test]public void Facade_for_tests()
7374
{
7475
var container = new Container();
7576

@@ -177,8 +178,7 @@ public void Example()
177178
class Without_singletons
178179
{
179180
public class S { }
180-
[Test]
181-
public void Example()
181+
[Test]public void Example()
182182
{
183183
IContainer container = new Container();
184184
container.Register<S>(Reuse.Singleton);
@@ -200,15 +200,13 @@ The method will clone the container registrations but will drop the cache.
200200
`WithRegistrationsCopy` will create a container clone (child) where the new registration will be isolated from the parent
201201
and the vice versa.
202202
203-
md*/
204-
203+
```cs md*/
205204
class With_registrations_copy
206205
{
207206
class A { }
208207
class B { public B(A a) {} }
209208

210-
[Test]
211-
public void Example()
209+
[Test]public void Example()
212210
{
213211
var parent = new Container();
214212
parent.Register<A>();
@@ -225,7 +223,8 @@ public void Example()
225223
var parentB = parent.Resolve<B>(IfUnresolved.ReturnDefaultIfNotRegistered);
226224
Assert.IsNull(parentB);
227225
}
228-
}/*md
226+
}
227+
/*md
229228
```
230229
231230
Again, the cloning here is the fast `O(1)` operation.
@@ -239,4 +238,56 @@ so the cache from the parent will proceed to be valid and useful.
239238
240239
[Explained in detail here](FaqAutofacMigration#separate-build-stage)
241240
241+
242+
## Scenarios from the actual Users
243+
244+
### The child container for each test disposed at end of the test without disposing the parent
245+
246+
[The related case](https://github.com/dadhi/DryIoc/issues/269)
247+
248+
```cs md*/
249+
250+
class Child_container_per_test_disposed_at_the_end_without_disposing_the_parent
251+
{
252+
[Test]public void Child_lifecycle_should_be_independent_of_parent_lifecycle()
253+
{
254+
var parent = new Container(rules => rules.WithConcreteTypeDynamicRegistrations());
255+
256+
parent.Register<IService, Service>(Reuse.Singleton);
257+
258+
var child = CreateChildContainer(parent);
259+
260+
// child can override parent registrations and parent is unchanged
261+
child.Use<IService>(new TestService());
262+
263+
Assert.IsInstanceOf<TestService>(child.Resolve<Concrete>().Service);
264+
Assert.IsInstanceOf<Service>(parent.Resolve<Concrete>().Service);
265+
266+
child.Dispose();
267+
Assert.IsTrue(child.IsDisposed);
268+
269+
// when child is disposed parent is unaffected
270+
Assert.IsFalse(parent.IsDisposed);
271+
Assert.IsInstanceOf<Service>(parent.Resolve<Concrete>().Service);
272+
}
273+
274+
private static IContainer CreateChildContainer(Container parent) =>
275+
parent.With(
276+
parent.Rules,
277+
parent.ScopeContext,
278+
RegistrySharing.CloneAndDropCache,
279+
parent.SingletonScope.Clone());
280+
281+
public interface IService { }
282+
public class Service : IService { }
283+
public class TestService : IService { }
284+
public class Concrete
285+
{
286+
public IService Service { get; }
287+
public Concrete(IService service) => Service = service;
288+
}
289+
}
290+
291+
/*md
292+
```
242293
md*/

docs/DryIoc.Docs/KindsOfChildContainer.md

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
- [Without Singletons](#without-singletons)
1212
- [With registrations copy](#with-registrations-copy)
1313
- [With no more registration allowed](#with-no-more-registration-allowed)
14+
- [Scenarios from the actual Users](#scenarios-from-the-actual-users)
15+
- [The child container for each test disposed at end of the test without disposing the parent](#the-child-container-for-each-test-disposed-at-end-of-the-test-without-disposing-the-parent)
1416

1517

1618
## No child containers
@@ -67,8 +69,7 @@ class FacadeExample
6769
}
6870
}
6971

70-
[Test]
71-
public void Facade_for_tests()
72+
[Test]public void Facade_for_tests()
7273
{
7374
var container = new Container();
7475

@@ -175,8 +176,7 @@ To remove resolved singleton instances from the container:
175176
class Without_singletons
176177
{
177178
public class S { }
178-
[Test]
179-
public void Example()
179+
[Test]public void Example()
180180
{
181181
IContainer container = new Container();
182182
container.Register<S>(Reuse.Singleton);
@@ -198,14 +198,13 @@ The method will clone the container registrations but will drop the cache.
198198
`WithRegistrationsCopy` will create a container clone (child) where the new registration will be isolated from the parent
199199
and the vice versa.
200200

201-
201+
```cs
202202
class With_registrations_copy
203203
{
204204
class A { }
205205
class B { public B(A a) {} }
206206

207-
[Test]
208-
public void Example()
207+
[Test]public void Example()
209208
{
210209
var parent = new Container();
211210
parent.Register<A>();
@@ -236,3 +235,54 @@ so the cache from the parent will proceed to be valid and useful.
236235

237236
[Explained in detail here](FaqAutofacMigration#separate-build-stage)
238237

238+
239+
## Scenarios from the actual Users
240+
241+
### The child container for each test disposed at end of the test without disposing the parent
242+
243+
[The related case](https://github.com/dadhi/DryIoc/issues/269)
244+
245+
```cs
246+
247+
class Child_container_per_test_disposed_at_the_end_without_disposing_the_parent
248+
{
249+
[Test]public void Child_lifecycle_should_be_independent_of_parent_lifecycle()
250+
{
251+
var parent = new Container(rules => rules.WithConcreteTypeDynamicRegistrations());
252+
253+
parent.Register<IService, Service>(Reuse.Singleton);
254+
255+
var child = CreateChildContainer(parent);
256+
257+
// child can override parent registrations and parent is unchanged
258+
child.Use<IService>(new TestService());
259+
260+
Assert.IsInstanceOf<TestService>(child.Resolve<Concrete>().Service);
261+
Assert.IsInstanceOf<Service>(parent.Resolve<Concrete>().Service);
262+
263+
child.Dispose();
264+
Assert.IsTrue(child.IsDisposed);
265+
266+
// when child is disposed parent is unaffected
267+
Assert.IsFalse(parent.IsDisposed);
268+
Assert.IsInstanceOf<Service>(parent.Resolve<Concrete>().Service);
269+
}
270+
271+
private static IContainer CreateChildContainer(Container parent) =>
272+
parent.With(
273+
parent.Rules,
274+
parent.ScopeContext,
275+
RegistrySharing.CloneAndDropCache,
276+
parent.SingletonScope.Clone());
277+
278+
public interface IService { }
279+
public class Service : IService { }
280+
public class TestService : IService { }
281+
public class Concrete
282+
{
283+
public IService Service { get; }
284+
public Concrete(IService service) => Service = service;
285+
}
286+
}
287+
288+
```

0 commit comments

Comments
 (0)