- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 125
Dispose child container without disposing parent #269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Related #259. |
Thanks. So, it doesn't seem that it is possible at the moment. I look forward to hearing if this is the intended design or whether you might change it in the future. |
Check a more generic
There is a way to add lightweight "registration" directly to scope via var parent = new Container();
using (var test = parent.OpenScope())
{
test.Use<IService>(new MockService());
} |
OK, that's worth giving a go. Thanks again for the fast responses. Always appreciated. |
Just to update, your suggestions of using the more generic .With(...) method from 259 worked thanks. I can create a child container, override any registrations I want, and then when I dispose the child the parent is no longer disposed as well. [Test]
public void child_lifecycle_should_be_independent_of_parent_lifecycle()
{
var parent = new Container(rules => rules
.WithConcreteTypeDynamicRegistrations());
parent.Register<IService, Service>(Reuse.Singleton);
// child can override parent registrations and parent is unchanged
var child = CreateChildContainer(parent);
child.UseInstance<IService>(new TestService(), IfAlreadyRegistered.Replace);
child.Resolve<Concrete>().Service.ShouldBeOfType<TestService>();
parent.Resolve<Concrete>().Service.ShouldBeOfType<Service>();
// when child is disposed parent is unaffected
child.Dispose();
child.IsDisposed.ShouldBeTrue();
parent.IsDisposed.ShouldBeFalse();
}
private IContainer CreateChildContainer(Container parent)
{
var child = parent.With(
parent.Rules,
parent.ScopeContext,
RegistrySharing.CloneAndDropCache,
parent.SingletonScope.Clone());
return child;
}
public interface IService { }
public class Service : IService { }
public class TestService : IService { }
public class Concrete
{
public IService Service { get; }
public Concrete(IService service)
{
Service = service;
}
} Thanks again for your help and for the excellent framework. |
@mwhelan Thanks for update, If you Ok I want to add this example to the docs. |
Yes, that’s fine with me. |
Hi
I am wanting to create the main container when my tests start, then create a child container for each test that is disposed at the end of the test. However, I was surprised to find that when I dispose the child container the parent is also disposed. Is there a way to change this behaviour?
Creating a child container with
parent.OpenScope()
works for disposing it without disposing the parent, but I don't think it lets me register items, which I would like to do.Any advice much appreciated! Thanks.
The text was updated successfully, but these errors were encountered: