本文整理汇总了C#中Container.CreateChildContainer方法的典型用法代码示例。如果您正苦于以下问题:C# Container.CreateChildContainer方法的具体用法?C# Container.CreateChildContainer怎么用?C# Container.CreateChildContainer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Container
的用法示例。
在下文中一共展示了Container.CreateChildContainer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: singletons_to_child_container_are_isolated
public void singletons_to_child_container_are_isolated()
{
var parentContainer = new Container(_ =>
{
_.For<IDependency>().Use<Dependency>();
});
var child1 = parentContainer.CreateChildContainer();
child1.Configure(x =>
{
x.ForSingletonOf<IRoot>().Use<Root>();
});
var child2 = parentContainer.CreateChildContainer();
child2.Configure(x =>
{
x.ForSingletonOf<IRoot>().Use<Root>();
});
// IRoot is a "singleton" within child1 usage
child1.GetInstance<IRoot>().ShouldBeSameAs(child1.GetInstance<IRoot>());
// IRoot is a "singleton" within child2 usage
child2.GetInstance<IRoot>().ShouldBeSameAs(child2.GetInstance<IRoot>());
// but, child1 and child2 both have a different IRoot
child1.GetInstance<IRoot>()
.ShouldNotBeTheSameAs(child2);
}
示例2: show_a_child_container_in_action
public void show_a_child_container_in_action()
{
var parent = new Container(_ =>
{
_.For<IWidget>().Use<AWidget>();
_.For<IService>().Use<AService>();
});
// Create a child container and override the
// IService registration
var child = parent.CreateChildContainer();
child.Configure(_ =>
{
_.For<IService>().Use<ChildSpecialService>();
});
// The child container has a specific registration
// for IService, so use that one
child.GetInstance<IService>()
.ShouldBeOfType<ChildSpecialService>();
// The child container does not have any
// override of IWidget, so it uses its parent's
// configuration to resolve IWidget
child.GetInstance<IWidget>()
.ShouldBeOfType<AWidget>();
}
示例3: SetUp
public void SetUp()
{
parent = new Container(_ => {
_.For<IWidget>().Use<AWidget>();
_.For<IService>().Use<AService>();
});
child = parent.CreateChildContainer();
}
示例4: ChildContainerCanReuseRegistrationsOnParent
public void ChildContainerCanReuseRegistrationsOnParent()
{
var container = new Container();
container.Register<IFoo>(c => new Foo());
var child = container.CreateChildContainer();
var foo = child.Resolve<IFoo>();
Assert.IsNotNull(foo);
}
示例5: container_scoping_with_root_child_and_nested_container
public void container_scoping_with_root_child_and_nested_container()
{
var container = new Container(_ =>
{
_.ForConcreteType<Disposable>().Configure.ContainerScoped();
});
var child = container.CreateChildContainer();
var nested = container.GetNestedContainer();
// Always the same object when requested from the root container
var mainDisposable = container.GetInstance<Disposable>();
mainDisposable
.ShouldBeTheSameAs(container.GetInstance<Disposable>());
// Always the same object when requested from a child container
var childDisposable = child.GetInstance<Disposable>();
childDisposable
.ShouldBeTheSameAs(child.GetInstance<Disposable>());
// Always the same object when requested from a nested container
var nestedDisposable = nested.GetInstance<Disposable>();
nestedDisposable
.ShouldBeTheSameAs(nested.GetInstance<Disposable>());
// It should be a different object instance for
// all three containers
mainDisposable
.ShouldNotBeTheSameAs(childDisposable);
mainDisposable
.ShouldNotBeTheSameAs(nestedDisposable);
childDisposable
.ShouldNotBeTheSameAs(nestedDisposable);
// When the nested container is disposed,
// it should dispose all the container scoped objects,
// but not impact the other containers
nested.Dispose();
nestedDisposable.WasDisposed.ShouldBeTrue();
childDisposable.WasDisposed.ShouldBeFalse();
mainDisposable.WasDisposed.ShouldBeFalse();
// Same for the child container
child.Dispose();
childDisposable.WasDisposed.ShouldBeTrue();
mainDisposable.WasDisposed.ShouldBeFalse();
// Same for the main container
container.Dispose();
mainDisposable.WasDisposed.ShouldBeTrue();
}
示例6: policies_should_apply_to_child_containers
public void policies_should_apply_to_child_containers()
{
var container = new Container(x =>
{
x.For<IWidget>().Use<AWidget>();
x.For<Activateable>()
.OnCreationForAll("Mark the object as activated", o => o.Activated = true);
});
container.CreateChildContainer().GetInstance<IWidget>()
.ShouldBeOfType<AWidget>()
.Activated.ShouldBeTrue();
}
示例7: child_and_nested_container_usage_of_singletons
public void child_and_nested_container_usage_of_singletons()
{
var container = new Container();
var child = container.CreateChildContainer();
child.Configure(_ => { _.ForSingletonOf<IColorCache>().Use<ColorCache>(); });
var singleton = child.GetInstance<IColorCache>();
// SingletonThing's should be resolved from the child container
using (var nested = child.GetNestedContainer())
{
// Fails, nested gets it's own instance
Assert.Same(singleton, nested.GetInstance<IColorCache>());
}
}
开发者ID:khellang,项目名称:structuremap,代码行数:15,代码来源:Bug_461_Child_Container_to_Nested_Container_Singleton_Bug.cs
示例8: ChildContainerInstanceWithParentRegistrationIsDisposed
public void ChildContainerInstanceWithParentRegistrationIsDisposed()
{
var parent = new Container();
parent.Register<IFoo>(c => new Disposable())
.ReusedWithin(ReuseScope.Hierarchy)
.OwnedBy(Owner.Container);
var child = parent.CreateChildContainer();
var foo = child.Resolve<IFoo>() as Disposable;
child.Dispose();
Assert.IsFalse(foo.IsDisposed);
}
示例9: disposing_a_child_container_disposes_the_objects_it_has_created_by_not_the_parent_created_objects
public void disposing_a_child_container_disposes_the_objects_it_has_created_by_not_the_parent_created_objects()
{
var root = new Container(_ => _.TransientTracking = TransientTracking.ExplicitReleaseMode);
var child = root.CreateChildContainer();
child.TransientTracking.ShouldNotBeSameAs(root.TransientTracking);
child.TransientTracking.ShouldBeOfType<TrackingTransientCache>();
var transient1 = root.GetInstance<DisposableGuy>();
var transient2 = child.GetInstance<DisposableGuy>();
child.Dispose();
transient1.WasDisposed.ShouldBeFalse();
transient2.WasDisposed.ShouldBeTrue();
}
示例10: should_be_the_same_in_the_nested_as_in_the_child_container
public void should_be_the_same_in_the_nested_as_in_the_child_container()
{
var container = new Container();
var child = container.CreateChildContainer();
child.Configure(_ =>
{
_.ForSingletonOf<IWidget>().Use<AWidget>();
});
var instance1 = child.GetInstance<IWidget>();
var instance2 = child.GetNestedContainer().GetInstance<IWidget>();
instance1.ShouldBeTheSameAs(instance2);
}
示例11: in_testing
public void in_testing()
{
var container = new Container(_ =>
{
_.For<IService>().Use<TheRealService>();
});
// Create a new child container for only this test
var testContainer = container.CreateChildContainer();
// Override a service with a stub that's easier to control
var stub = new StubbedService();
testContainer.Inject<IService>(stub);
// Now, continue with the test resolving application
// services through the new child container....
}
示例12: should_be_able_to_instantiate_root
public void should_be_able_to_instantiate_root()
{
var parentContainer = new Container();
var childContainer = parentContainer.CreateChildContainer();
childContainer.Configure(x =>
{
x.ForSingletonOf<IRoot>().Use<Root>();
x.For<IDependency>().Use<Dependency>();
});
var dependency = childContainer.GetInstance<IDependency>(); // Works
// Fixed
childContainer.GetInstance<IRoot>().ShouldNotBeNull(); // Fails
childContainer.Model.For<IRoot>().Lifecycle.ShouldBeOfType<ContainerLifecycle>();
}
示例13: nested_container_from_child
public void nested_container_from_child()
{
var parent = new Container(_ =>
{
_.For<IWidget>().Use<AWidget>();
_.For<IService>().Use<AService>();
});
// Create a child container and override the
// IService registration
var child = parent.CreateChildContainer();
child.Configure(_ =>
{
_.For<IService>().Use<ChildSpecialService>();
});
using (var nested = child.GetNestedContainer())
{
nested.GetInstance<IService>()
.ShouldBeOfType<ChildSpecialService>();
}
}
示例14: TryResolveReturnsRegisteredInstanceOnParent
public void TryResolveReturnsRegisteredInstanceOnParent()
{
var container = new Container();
container.Register<Bar>("bar", c => new Bar());
container.Register<Bar, string>("bar", (c, s) => new Bar(s));
container.Register<Bar, string, string>("bar", (c, s1, s2) => new Bar(s1, s2));
container.Register<Bar, string, string, string>("bar", (c, s1, s2, s3) => new Bar(s1, s2, s3));
container.Register<Bar, string, string, string, string>("bar", (c, s1, s2, s3, s4) => new Bar(s1, s2, s3, s4));
container.Register<Bar, string, string, string, string, string>("bar", (c, s1, s2, s3, s4, s5) => new Bar(s1, s2, s3, s4, s5));
container.Register<Bar, string, string, string, string, string, string>("bar", (c, s1, s2, s3, s4, s5, s6) => new Bar(s1, s2, s3, s4, s5, s6));
var child = container.CreateChildContainer();
Assert.IsNotNull(child.TryResolveNamed<Bar>("bar"));
Assert.IsNotNull(child.TryResolveNamed<Bar, string>("bar", "a1"));
Assert.IsNotNull(child.TryResolveNamed<Bar, string, string>("bar", "a1", "a2"));
Assert.IsNotNull(child.TryResolveNamed<Bar, string, string, string>("bar", "a1", "a2", "a3"));
Assert.IsNotNull(child.TryResolveNamed<Bar, string, string, string, string>("bar", "a1", "a2", "a3", "a4"));
Assert.IsNotNull(child.TryResolveNamed<Bar, string, string, string, string, string>("bar", "a1", "a2", "a3", "a4", "a5"));
Assert.IsNotNull(child.TryResolveNamed<Bar, string, string, string, string, string, string>("bar", "a1", "a2", "a3", "a4", "a5", "a6"));
}
示例15: ShouldGetSameContainerServiceAsCurrentContainer
public void ShouldGetSameContainerServiceAsCurrentContainer()
{
var container = new Container();
var service = container.Resolve<Container>();
Assert.AreSame(container, service);
var child = container.CreateChildContainer();
service = child.Resolve<Container>();
Assert.AreSame(child, service);
var grandChild = child.CreateChildContainer();
service = grandChild.Resolve<Container>();
Assert.AreSame(grandChild, service);
}