本文整理汇总了C#中Container.GetNestedContainer方法的典型用法代码示例。如果您正苦于以下问题:C# Container.GetNestedContainer方法的具体用法?C# Container.GetNestedContainer怎么用?C# Container.GetNestedContainer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Container
的用法示例。
在下文中一共展示了Container.GetNestedContainer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: can_override_lifecycle_at_instance
public void can_override_lifecycle_at_instance()
{
var container = new Container(x => {
x.For<Rule>().Singleton();
x.For<Rule>().Add<ARule>().Named("A").Transient();
x.For<Rule>().Add<ARule>().Named("B").AlwaysUnique();
x.For<Rule>().Add<ARule>().Named("C");
});
container.Model.For<Rule>().Lifecycle.ShouldBeOfType<SingletonLifecycle>();
container.Model.For<Rule>().Find("C").Lifecycle.ShouldBeOfType<SingletonLifecycle>();
// 'C' is the default lifecycle for Rule (Singleton)
container.GetInstance<Rule>("C")
.ShouldBeTheSameAs(container.GetInstance<Rule>("C"));
// 'A' is a transient
container.GetInstance<Rule>("A")
.ShouldNotBeTheSameAs(container.GetInstance<Rule>("A"));
using (var nested = container.GetNestedContainer())
{
// 'B' is always unique
nested.GetInstance<Rule>("B")
.ShouldNotBeTheSameAs(nested.GetInstance<Rule>("B"));
}
}
示例2: setter_policies_should_be_applied_in_nested_container
public void setter_policies_should_be_applied_in_nested_container()
{
var container = new Container(cfg => { cfg.Policies.SetAllProperties(x => x.OfType<Injected>()); });
var product = container.GetNestedContainer().GetInstance<Product>();
product.Inject.ShouldNotBeNull();
}
开发者ID:khellang,项目名称:structuremap,代码行数:7,代码来源:Setter_policies_when_the_build_plan_is_created_by_a_nested_container.cs
示例3: Always_Unique
public void Always_Unique()
{
var c = new Container(x => { x.For<IService>().Use<Service>().AlwaysUnique(); });
// In a normal container, you get a new object
// instance of the Service class in subsequent
// requests
c.GetInstance<IService>()
.ShouldNotBeTheSameAs(c.GetInstance<IService>())
.ShouldNotBeTheSameAs(c.GetInstance<IService>());
// Within a nested container, 'Transient' now
// means within the Nested Container.
// A nested container is effectively one request
using (var nested = c.GetNestedContainer())
{
nested.GetInstance<IService>()
.ShouldNotBeTheSameAs(nested.GetInstance<IService>())
.ShouldNotBeTheSameAs(nested.GetInstance<IService>());
}
// Even in a single request,
var holder = c.GetInstance<ServiceUserHolder>();
holder.Service.ShouldNotBeTheSameAs(holder.User.Service);
}
示例4: get_a_nested_container_for_a_profile
public void get_a_nested_container_for_a_profile()
{
var parent = new Container(x =>
{
x.For<IWidget>().Use<ColorWidget>()
.Ctor<string>("color").Is("red");
x.Profile("green", o =>
{
o.For<IWidget>().Use<ColorWidget>()
.Ctor<string>("color").Is("green");
});
});
var child = parent.GetNestedContainer("green");
var childWidget1 = child.GetInstance<IWidget>();
var childWidget2 = child.GetInstance<IWidget>();
var childWidget3 = child.GetInstance<IWidget>();
var parentWidget = parent.GetInstance<IWidget>();
childWidget1.ShouldBeTheSameAs(childWidget2);
childWidget1.ShouldBeTheSameAs(childWidget3);
childWidget1.ShouldNotBeTheSameAs(parentWidget);
parentWidget.ShouldBeOfType<ColorWidget>().Color.ShouldBe("red");
childWidget1.ShouldBeOfType<ColorWidget>().Color.ShouldBe("green");
}
示例5: NestedContainer_names_should_start_with_nested_container
public void NestedContainer_names_should_start_with_nested_container()
{
var container = new Container(x => x.For<IAutomobile>().Use<Mustang>());
var nestedContainer = container.GetNestedContainer();
nestedContainer.Name.ShouldStartWith("Nested-");
}
示例6: nested_container_behavior_of_transients
public void nested_container_behavior_of_transients()
{
// "Transient" is the default lifecycle
// in StructureMap
var container = new Container(_ => {
_.For<IColor>().Use<Green>();
});
// In a normal Container, a "transient" lifecycle
// Instance will be built up once in every request
// to the Container
container.GetInstance<IColor>()
.ShouldNotBeTheSameAs(container.GetInstance<IColor>());
// From a nested container, the "transient" lifecycle
// is tracked to the nested container
using (var nested = container.GetNestedContainer())
{
nested.GetInstance<IColor>()
.ShouldBeTheSameAs(nested.GetInstance<IColor>());
// One more time
nested.GetInstance<IColor>()
.ShouldBeTheSameAs(nested.GetInstance<IColor>());
}
}
示例7: singletons_are_created_in_a_completely_separate_context_in_the_parent_container
public void singletons_are_created_in_a_completely_separate_context_in_the_parent_container()
{
var container = new Container(x =>
{
x.ForSingletonOf<ISingletonThing>().Use<SingletonThing>();
x.For<ITransient>().Use<Transient>();
});
Thing thing = null;
using (var nested = container.GetNestedContainer())
{
thing = nested.GetInstance<Thing>();
}
// The transient object on Thing should be disposed
thing.Transient.ShouldBeOfType<Transient>().WasDisposed.ShouldBeTrue();
// SingletonThing should be created in the master scope and therefore,
// not disposed by the nested container closing
thing.SingletonThing.ShouldBeOfType<SingletonThing>()
.Transient.ShouldBeOfType<Transient>()
.WasDisposed.ShouldBeFalse();
// The SingletonThing should not be sharing any children services with the transient
thing.Transient.ShouldNotBeTheSameAs(thing.SingletonThing.ShouldBeOfType<SingletonThing>().Transient);
}
示例8: do_not_allow
public void do_not_allow()
{
var container = new Container();
Exception<StructureMapConfigurationException>.ShouldBeThrownBy(() => {
container.GetNestedContainer().Configure(_ => {
_.Policies.FillAllPropertiesOfType<IWidget>();
});
});
}
开发者ID:goraw,项目名称:structuremap,代码行数:10,代码来源:do_not_allow_policy_changes_in_nested_container_issue_284.cs
示例9: allow_nested_container_to_report_what_it_has
public void allow_nested_container_to_report_what_it_has()
{
var container = new Container(x => x.For<IAutomobile>().Use<Mustang>());
var nestedContainer = container.GetNestedContainer();
nestedContainer.Inject<IEngine>(new PushrodEngine());
container.WhatDoIHave().ShouldNotBeEmpty().ShouldNotContain(typeof(IEngine).Name);
nestedContainer.WhatDoIHave().ShouldNotBeEmpty().ShouldContain(typeof(IEngine).Name);
}
示例10: disposing_a_nested_container_does_not_try_to_dispose_objects_created_by_the_parent
public void disposing_a_nested_container_does_not_try_to_dispose_objects_created_by_the_parent()
{
var container = new Container(x => { x.ForSingletonOf<I1>().Use<C1No>(); });
var child = container.GetNestedContainer();
// Blows up if the Dispose() is called
var notDisposable = child.GetInstance<I1>();
child.Dispose();
}
示例11: 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();
}
示例12: nested_container
public void nested_container()
{
var someExistingContainer = new Container();
// SAMPLE: glossary-nested-container
using (var nested = someExistingContainer.GetNestedContainer())
{
// pull other objects from the nested container and do work with those services
var service = nested.GetInstance<IService>();
service.DoSomething();
}
// ENDSAMPLE
}
示例13: creating_a_nested_container
public void creating_a_nested_container()
{
// From an IContainer object
var container = new Container(_ => { _.For<IWorker>().Use<Worker>(); });
using (var nested = container.GetNestedContainer())
{
// This object is disposed when the nested container
// is disposed
var worker = nested.GetInstance<IWorker>();
worker.DoWork();
}
}
示例14: policies_should_apply_to_nested_containers
public void policies_should_apply_to_nested_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.GetNestedContainer().GetInstance<IWidget>()
.ShouldBeOfType<AWidget>()
.Activated.ShouldBeTrue();
}
示例15: nested_container_usage_of_singletons
public void nested_container_usage_of_singletons()
{
var container = new Container(_ => { _.ForSingletonOf<IColorCache>().Use<ColorCache>(); });
var singleton = container.GetInstance<IColorCache>();
// SingletonThing's are resolved from the parent container
using (var nested = container.GetNestedContainer())
{
nested.GetInstance<IColorCache>()
.ShouldBeTheSameAs(singleton);
}
}