本文整理汇总了C#中System.IocContainer.Compile方法的典型用法代码示例。如果您正苦于以下问题:C# IocContainer.Compile方法的具体用法?C# IocContainer.Compile怎么用?C# IocContainer.Compile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IocContainer
的用法示例。
在下文中一共展示了IocContainer.Compile方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompileCanInlineRegistrationsWithContainerLifetime
public void CompileCanInlineRegistrationsWithContainerLifetime()
{
// Doesnt check that Compile actually works - just that it doesnt break anything
using (var container = new IocContainer())
{
container.Register<IFoo>(x => new Foo1()).ContainerLifetime();
container.Register<IBar>(x => new Bar1());
var reg = container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>(), x.Resolve<IBar>()));
container.Compile();
var result1 = container.Resolve<IFoo>();
var result2 = (IFooBar)container.Resolve(reg);
Assert.AreEqual(result1, result2.Foo);
}
}
示例2: CompileCanInlineInstanceRegistrations
public void CompileCanInlineInstanceRegistrations()
{
// Doesnt check that Compile actually works - just that it doesnt break anything
using (var container = new IocContainer())
{
var i1 = new Foo1();
var i2 = new Bar1();
container.RegisterInstance(i1);
container.RegisterInstance(i2);
var reg1 = container.Register<IFooBar>(x => new FooBar(x.Resolve<Foo1>(), x.Resolve<Bar1>()));
container.Compile();
var result = (IFooBar)container.Resolve(reg1);
Assert.AreEqual(i1, result.Foo);
}
}
示例3: CompileUsingKeyDoesntBreakUsingCompileModeDynamicWhenUsingInstanceRegistrationOrContainerLifetime
public void CompileUsingKeyDoesntBreakUsingCompileModeDynamicWhenUsingInstanceRegistrationOrContainerLifetime()
{
// Test that InstanceRegistration and ContainerLifetime works using CompileMode Dynamic (should not be inlined)
using (var container = new IocContainer(defaultCompileMode: CompileMode.Dynamic))
{
var barInstance = new Bar1();
var reg1 = container.Register<IFoo, Foo1>("FirstKey").WithContainerLifetime();
var reg2 = container.RegisterInstance<IBar>(barInstance, "SecondKey");
var reg3 = container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>("FirstKey"), x.Resolve<IBar>("SecondKey")));
container.Compile();
var res1 = container.Resolve<IFooBar>();
var res2 = container.Resolve<IFooBar>();
// Assert
Assert.AreNotSame(res1, res2);
Assert.AreSame(barInstance, res1.Bar);
Assert.AreSame(res1.Foo, res2.Foo);
}
}
示例4: RegisterAutoWorksWithCompileModeDynamicAndInternalProtectedCtor
public void RegisterAutoWorksWithCompileModeDynamicAndInternalProtectedCtor()
{
using (var container = new IocContainer())
{
var reg1 = container.Register(typeof(IFoo), typeof(InternalProtectedFoo), compileMode: CompileMode.Dynamic, includeInternalCtor: true);
var reg2 = container.Register(typeof(IFoo), typeof(InternalProtectedFoo), "TheKey", compileMode: CompileMode.Dynamic, includeInternalCtor: true);
// Register as InternalProtectedFoo because default for IFoo is registered above
var reg3 = container.Register<InternalProtectedFoo, InternalProtectedFoo>(compileMode: CompileMode.Dynamic, includeInternalCtor: true);
var reg4 = container.Register<InternalProtectedFoo, InternalProtectedFoo>("TheKey", compileMode: CompileMode.Dynamic, includeInternalCtor: true);
container.Compile();
var instance1 = reg1.GetInstance();
var instance2 = reg2.GetInstance();
var instance3 = reg3.GetInstance();
var instance4 = reg4.GetInstance();
Assert.IsInstanceOfType(instance1, typeof(InternalProtectedFoo));
Assert.IsInstanceOfType(instance2, typeof(InternalProtectedFoo));
Assert.IsInstanceOfType(instance3, typeof(InternalProtectedFoo));
Assert.IsInstanceOfType(instance4, typeof(InternalProtectedFoo));
}
}
示例5: RegisterAutoWorksWithInternalCtor
public void RegisterAutoWorksWithInternalCtor()
{
using (var container = new IocContainer())
{
var reg1 = container.Register(typeof(IFoo), typeof(InternalFoo), compileMode: CompileMode.Delegate, includeInternalCtor: true);
var reg2 = container.Register(typeof(IFoo), typeof(InternalFoo), "TheKey", compileMode: CompileMode.Delegate, includeInternalCtor: true);
var reg3 = container.Register<InternalFoo, InternalFoo>(compileMode: CompileMode.Delegate, includeInternalCtor: true);
var reg4 = container.Register<InternalFoo, InternalFoo>("TheKey", compileMode: CompileMode.Delegate, includeInternalCtor: true);
container.Compile();
var instance1 = reg1.GetInstance();
var instance2 = reg2.GetInstance();
var instance3 = reg3.GetInstance();
var instance4 = reg4.GetInstance();
Assert.IsInstanceOfType(instance1, typeof(InternalFoo));
Assert.IsInstanceOfType(instance2, typeof(InternalFoo));
Assert.IsInstanceOfType(instance3, typeof(InternalFoo));
Assert.IsInstanceOfType(instance4, typeof(InternalFoo));
}
}
示例6: CompileWorksWithPropertyInjection
public void CompileWorksWithPropertyInjection()
{
using (var container = new IocContainer())
{
// Arrange
container.Register<IFoo>(c => new Foo1());
container.Register<IBar>(c => new Bar1());
container.Register<IFooBar>(c => new FooBar() { Foo = c.Resolve<IFoo>(), Bar = c.Resolve<IBar>() });
// Act
container.Compile();
var foo = container.Resolve<IFoo>();
var bar = container.Resolve<IBar>();
var foobar = container.Resolve<IFooBar>();
// Assert
Assert.IsNotNull(foo);
Assert.IsNotNull(bar);
Assert.IsNotNull(foobar);
Assert.IsNotNull(foobar.Foo);
Assert.IsNotNull(foobar.Bar);
}
}
示例7: CompileThrowsExceptionIfRegistrationsAreNotValid
public void CompileThrowsExceptionIfRegistrationsAreNotValid()
{
using (var container = new IocContainer())
{
// Arrange
container.Register<IFoo>(c => new Foo1());
container.Register<IBar>(x => new Bar1(), "Bar");
container.Register<IFooBar>(x => new FooBar((IFoo)x.Resolve(typeof(IFoo)), (IBar)x.Resolve(typeof(IBar), "Bar")));
container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>(), x.Resolve<IBar>()), "FooBar");
// Act
container.Compile();
}
}
示例8: CompileProtectsAgainstInfiniteLoop
public void CompileProtectsAgainstInfiniteLoop()
{
using (var container = new IocContainer())
{
// Arrange
container.Register<IFoo>(c => new Foo1());
container.Register<IBar>(x => new Bar2(x.Resolve<IFooBar>()));
container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>(), x.Resolve<IBar>()));
// Act
container.Compile();
}
}
示例9: CompileDoesntBreakAnything
public void CompileDoesntBreakAnything()
{
using (var container = new IocContainer())
{
// Arrange
container.Register<IFoo>(c => new Foo1());
container.Register<IBar>(x => new Bar1(), "BarKey");
container.Register<IFooBar>(y => new FooBar((IFoo)y.Resolve(typeof(IFoo)), (IBar)y.Resolve(typeof(IBar), "BarKey")));
container.Register<IFooBar>(xyz => new FooBar(xyz.Resolve<IFoo>(), xyz.Resolve<IBar>("BarKey")), "FooBarKey");
// Act
container.Compile();
var foo = container.Resolve<IFoo>();
var bar = container.Resolve<IBar>("BarKey");
var foobar1 = container.Resolve<IFooBar>();
var foobar2 = container.Resolve<IFooBar>("FooBarKey");
// Assert
Assert.IsNotNull(foo);
Assert.IsNotNull(bar);
Assert.IsNotNull(foobar1);
Assert.IsNotNull(foobar2);
Assert.IsNotNull(foobar1.Foo, null);
Assert.IsNotNull(foobar1.Bar, null);
Assert.IsNotNull(foobar2.Foo, null);
Assert.IsNotNull(foobar2.Bar, null);
}
}
示例10: RequestLifetimeWorksWithCompile
public void RequestLifetimeWorksWithCompile()
{
using (var container = new IocContainer())
{
// Arrange
var context = new FakeHttpContext("Http://fakeUrl1.com");
var requestLifetime = new RequestLifetime(() => context, disposeOnEnd: false); // Do not test disposeOnEnd here
container.Register<IFoo, Foo1>();
container.Register<IBar, Bar1>().SetLifetime(requestLifetime);
container.Register<IFooBar, FooBar>();
// Act
container.Compile();
var fooBar = container.Resolve<IFooBar>();
// Assert
Assert.IsNotNull(fooBar);
Assert.IsNotNull(fooBar.Foo);
Assert.IsNotNull(fooBar.Bar);
}
}
示例11: RequestLifetimeNestedWorksWithCompile
public void RequestLifetimeNestedWorksWithCompile()
{
// This doesnt work - will affect other Lifetimes that just nests the expression
// Need to fix the scope of the input parameter IResolver
using (var container = new IocContainer())
{
// Arrange
var context = new FakeHttpContext("Http://fakeUrl1.com");
var requestLifetime = new RequestLifetime(() => context, disposeOnEnd: false); // Do not test disposeOnEnd here
container.Register<IFoo, Foo1>();
container.Register<IBar, Bar1>().SetLifetime(requestLifetime);
container.Register<IFooBar, FooBar>();
container.Register<IFooBarContainer, FooBarContainer>();
// Act
container.Compile();
var fooBarContainer = container.Resolve<IFooBarContainer>();
// Assert
Assert.IsNotNull(fooBarContainer);
Assert.IsNotNull(fooBarContainer.FooBar);
Assert.IsNotNull(fooBarContainer.FooBar.Foo);
Assert.IsNotNull(fooBarContainer.FooBar.Bar);
}
}
示例12: CompileWorksWhenCompiledMultipleTimes
public void CompileWorksWhenCompiledMultipleTimes()
{
using (var container = new IocContainer())
{
// Arrange
var foo = new Foo1();
var bar = new Bar1();
container.RegisterInstance<IFoo>(foo);
container.RegisterInstance<IBar>(bar);
container.Register<IFooBar, FooBar>().WithContainerLifetime();
container.Register<IFooBarContainer, FooBarContainer>().WithContainerLifetime();
// Act
container.Compile();
container.Compile();
var fooBarContainer = container.Resolve<IFooBarContainer>();
// Assert
Assert.IsNotNull(fooBarContainer);
Assert.IsNotNull(fooBarContainer.FooBar);
Assert.IsNotNull(fooBarContainer.FooBar.Foo);
Assert.IsNotNull(fooBarContainer.FooBar.Bar);
Assert.AreEqual(foo, fooBarContainer.FooBar.Foo);
Assert.AreEqual(bar, fooBarContainer.FooBar.Bar);
}
}
示例13: CompileWorksWithNesting2
public void CompileWorksWithNesting2()
{
using (var container = new IocContainer())
{
// Arrange
container.Register<IFoo, Foo1>().SetLifetime(new TransientLifetime());
container.Register<IBar, Bar1>().SetLifetime(new ContainerLifetime());
container.Register<IFooBar, FooBar>().WithContainerLifetime();
container.Register<IFooBarContainer, FooBarContainer>().WithContainerLifetime();
// Act
container.Compile();
var fooBarContainer = container.Resolve<IFooBarContainer>();
// Assert
Assert.IsNotNull(fooBarContainer);
Assert.IsNotNull(fooBarContainer.FooBar);
Assert.IsNotNull(fooBarContainer.FooBar.Foo);
Assert.IsNotNull(fooBarContainer.FooBar.Bar);
}
}
示例14: CompileCanInlineRegistrationsWithTransientLifetime
public void CompileCanInlineRegistrationsWithTransientLifetime()
{
// Doesnt check that Compile actually works - just that it doesnt break anything
using (var container = new IocContainer())
{
container.Register<IFoo>(x => new Foo1()).WithTransientLifetime();
container.Register<IBar>(x => new Bar1());
var reg1 = container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>(), x.Resolve<IBar>()));
var reg2 = container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>(), x.Resolve<IBar>()), "Key");
container.Compile();
var result1 = container.Resolve<IFoo>();
var result2 = (IFooBar)reg1.GetInstance();
var result3 = (IFooBar)reg2.GetInstance();
Assert.AreNotEqual(result1, result2.Foo);
Assert.AreNotEqual(result1, result3.Foo);
Assert.AreNotEqual(result2.Foo, result3.Foo);
}
}