本文整理汇总了C#中ContainerBuilder.DefaultControlledBy方法的典型用法代码示例。如果您正苦于以下问题:C# ContainerBuilder.DefaultControlledBy方法的具体用法?C# ContainerBuilder.DefaultControlledBy怎么用?C# ContainerBuilder.DefaultControlledBy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ContainerBuilder
的用法示例。
在下文中一共展示了ContainerBuilder.DefaultControlledBy方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Instance_is_not_reused_in_different_httprequests
public void Instance_is_not_reused_in_different_httprequests()
{
var builder = new ContainerBuilder();
var currentItems = new Dictionary<string, object>();
var currentContext = new Mock<HttpContextBase>();
currentContext
.Setup(c => c.Items)
.Returns(currentItems);
builder.DefaultControlledBy(() => new HttpRequestLifecycle
{
CurrentContext = currentContext.Object
});
builder.Register<IBar, Bar>().ControlledBy<TransientLifecycle>();
builder.Register<IFoo, Foo>();
var container = builder.Build();
var firstResolvedInstanceInContext = container.Resolve<IFoo>();
currentItems = new Dictionary<string, object>();
// Simulate new request scope.
currentContext
.Setup(c => c.Items)
.Returns(currentItems);
var secondResolvedInstanceInContext = container.Resolve<IFoo>();
Assert.AreNotSame(firstResolvedInstanceInContext, secondResolvedInstanceInContext);
}
示例2: Instance_is_reused_on_same_thread_when_controlled_by_threadsingleton_lifecycle
public void Instance_is_reused_on_same_thread_when_controlled_by_threadsingleton_lifecycle()
{
var builder = new ContainerBuilder();
builder.DefaultControlledBy<ThreadSingletonLifecycle>();
builder.Register<IFoo, Foo>();
builder.Register<IBar, Bar>();
var container = builder.Build();
var threadData = new ThreadData(container);
var thread = new Thread(threadData.ResolveFoosWithContainer);
var threadDataTwo = new ThreadData(container);
var threadTwo = new Thread(threadDataTwo.ResolveFoosWithContainer);
thread.Start();
threadTwo.Start();
thread.Join();
threadTwo.Join();
Assert.IsTrue(ReferenceEquals(threadData.FooOne, threadData.FooTwo));
Assert.IsFalse(ReferenceEquals(threadData.FooOne, threadDataTwo.FooOne));
}
示例3: Container_resolves_properties_in_transient_lifecycle
public void Container_resolves_properties_in_transient_lifecycle()
{
var builder = new ContainerBuilder();
builder.Register<IDisposable>(c => new MemoryStream()).ControlledBy<TransientLifecycle>();
builder.DefaultControlledBy<TransientLifecycle>();
var container = builder.Build();
var streamContainerOne = container.Resolve<StreamContainer>();
var streamContainerTwo = container.Resolve<StreamContainer>();
Assert.IsFalse(ReferenceEquals(streamContainerOne, streamContainerTwo), "Root objects are equal");
Assert.IsFalse(ReferenceEquals(streamContainerOne.Stream, streamContainerTwo.Stream), "Contained objects are equal");
}
示例4: Instance_is_reused_when_controlled_by_singleton_lifecycle
public void Instance_is_reused_when_controlled_by_singleton_lifecycle()
{
var builder = new ContainerBuilder();
builder.DefaultControlledBy<SingletonLifecycle>();
builder.Register<IFoo, Foo>();
builder.Register<IBar, Bar>();
var container = builder.Build();
var foo1 = container.Resolve<IFoo>();
var foo2 = container.Resolve<IFoo>();
Assert.IsTrue(ReferenceEquals(foo1, foo2));
}
示例5: CointainerBuilder_default_scope_can_be_altered_to_singleton
public void CointainerBuilder_default_scope_can_be_altered_to_singleton()
{
var builder = new ContainerBuilder();
builder.DefaultControlledBy<SingletonLifecycle>();
builder.Register<IBar, Bar>();
builder.Register<IFoo, Foo>();
var container = builder.Build();
var instanceOne = container.Resolve<IFoo>();
var instanceTwo = container.Resolve<IFoo>();
Assert.AreSame(instanceOne, instanceTwo);
}
示例6: BuilderDefaultControlledBySetsAllObjectsToSingleton
public void BuilderDefaultControlledBySetsAllObjectsToSingleton()
{
var builder = new ContainerBuilder();
builder.DefaultControlledBy<SingletonLifecycle>(); // do this before the register
builder.Register<IJellybeanDispenser, VanillaJellybeanDispenser>();
builder.Register<SweetVendingMachine>();
builder.Register<SweetShop>();
IContainer container = builder.Build();
SweetShop sweetShop = container.Resolve<SweetShop>();
SweetShop sweetShop2 = container.Resolve<SweetShop>();
Assert.AreSame(sweetShop, sweetShop2, "Root objects are equal");
Assert.AreSame(sweetShop.SweetVendingMachine, sweetShop2.SweetVendingMachine, "Contained objects are equal");
Assert.AreSame(sweetShop.SweetVendingMachine.JellybeanDispenser, sweetShop2.SweetVendingMachine.JellybeanDispenser, "JellybeanDispenser are equal");
}
示例7: Registered_controller_instances_are_reused_on_httpcontextstrategy
public void Registered_controller_instances_are_reused_on_httpcontextstrategy()
{
var builder = new ContainerBuilder();
var registrationModule = new ControllerRegistrationModule(typeof(FooController).Assembly);
var currentItems = new Dictionary<string, object>();
var currentContext = new Mock<HttpContextBase>();
currentContext
.Setup(c => c.Items)
.Returns(currentItems);
builder.DefaultControlledBy(() => new HttpRequestLifecycle
{
CurrentContext = currentContext.Object
});
builder.RegisterModule(registrationModule);
var container = builder.Build();
var controllerFactory = new ControllerFactory(container);
var controller = controllerFactory.CreateControllerInternal(this.CreateRequestContext(), typeof(FooController));
var secondController = controllerFactory.CreateControllerInternal(this.CreateRequestContext(), typeof(FooController));
Assert.That(controller, Is.SameAs(secondController));
}
示例8: CointainerBuilder_default_scope_can_be_altered_to_singleton_with_delegate_activator_registration
public void CointainerBuilder_default_scope_can_be_altered_to_singleton_with_delegate_activator_registration()
{
var builder = new ContainerBuilder();
builder.DefaultControlledBy<SingletonLifecycle>();
builder.Register<IBar>(c => new Bar());
builder.Register<IFoo>(c => new Foo());
var container = builder.Build();
var instanceOne = container.Resolve<IFoo>();
var instanceTwo = container.Resolve<IFoo>();
Assert.AreSame(instanceOne, instanceTwo);
}