本文整理汇总了C#中IocContainer类的典型用法代码示例。如果您正苦于以下问题:C# IocContainer类的具体用法?C# IocContainer怎么用?C# IocContainer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IocContainer类属于命名空间,在下文中一共展示了IocContainer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenericResolveByTypeNotRegisteredThrowsException
public void GenericResolveByTypeNotRegisteredThrowsException()
{
using (var container = new IocContainer())
{
container.Resolve<IFoo>();
}
}
示例2: DefaultLifetimeMangerIsNull
public void DefaultLifetimeMangerIsNull()
{
using (var iocContainer = new IocContainer())
{
Assert.IsNull(iocContainer.DefaultLifetimeManager);
}
}
示例3: CanSetDefaultLifetimeToTransient
public void CanSetDefaultLifetimeToTransient()
{
using (var container = new IocContainer(() => new TransientLifetime()))
{
Assert.IsInstanceOfType(container.DefaultLifetime, typeof(TransientLifetime));
}
}
示例4: DefaultLifetimeIsDefaultSetToTransient
public void DefaultLifetimeIsDefaultSetToTransient()
{
using (var container = new IocContainer())
{
Assert.IsInstanceOfType(container.DefaultLifetime, typeof(TransientLifetime));
}
}
示例5: CanSetDefaultLifetimeToSessionLifetime
public void CanSetDefaultLifetimeToSessionLifetime()
{
using (var container = new IocContainer(() => new SessionLifetime()))
{
Assert.IsInstanceOfType(container.DefaultLifetimeFactory(), typeof(SessionLifetime));
}
}
示例6: MunqUseCase
static MunqUseCase()
{
container = new IocContainer();
singleton = new Munq.LifetimeManagers.ContainerLifetime();
container.Register<IWebService>(
c => new WebService(
c.Resolve<IAuthenticator>(),
c.Resolve<IStockQuote>()));
container.Register<IAuthenticator>(
c => new Authenticator(
c.Resolve<ILogger>(),
c.Resolve<IErrorHandler>(),
c.Resolve<IDatabase>()));
container.Register<IStockQuote>(
c => new StockQuote(
c.Resolve<ILogger>(),
c.Resolve<IErrorHandler>(),
c.Resolve<IDatabase>()));
container.Register<IDatabase>(
c => new Database(
c.Resolve<ILogger>(),
c.Resolve<IErrorHandler>()));
container.Register<IErrorHandler>(
c => new ErrorHandler(c.Resolve<ILogger>()));
container.RegisterInstance<ILogger>(new Logger())
.WithLifetimeManager(singleton);
}
示例7: SessionLifetimeReturnsSameInstanceForSameSessionAndDifferentInstanceForDifferentSession
public void SessionLifetimeReturnsSameInstanceForSameSessionAndDifferentInstanceForDifferentSession()
{
// Arrange
using (var container = new IocContainer())
{
var sessionItems1 = new SessionStateItemCollection();
var sessionItems2 = new SessionStateItemCollection();
var context1 = new FakeHttpContext("Http://fakeUrl1.com", null, null, null, null, sessionItems1);
var context2 = new FakeHttpContext("Http://fakeUrl2.com", null, null, null, null, sessionItems2);
HttpContextBase currentContext = null;
var lifetime = new SessionLifetime(() => currentContext); // better solution for injecting HttpContext ?
container.Register<IFoo>(c => new Foo1()).SetLifetime(lifetime);
// Act
currentContext = context1;
var result1 = container.Resolve<IFoo>();
var result2 = container.Resolve<IFoo>();
currentContext = context2;
var result3 = container.Resolve<IFoo>();
// Assert
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.IsNotNull(result3);
Assert.AreSame(result1, result2);
Assert.AreNotSame(result1, result3);
}
}
示例8: Configuration
public void Configuration(IAppBuilder appBuilder)
{
IIocContainer iocContainer = new IocContainer();
Bootstrap.Application()
.ResolveReferencesWith(iocContainer)
.UseEnvironment()
.UseDomain().WithSimpleMessaging()
.ConfigureRouteInspectorServer().WithRouteLimitOf(10)
.ConfigureRouteInspectorWeb()
.UseEventSourcing().PersistToMemory()
.Initialise();
var configuration = new HttpConfiguration
{
DependencyResolver = new SystemDotDependencyResolver(iocContainer)
};
configuration.MapHttpAttributeRoutes();
configuration.Filters.Add(new ModelStateContextFilterAttribute());
configuration.Filters.Add(new NoCacheFilterAttribute());
configuration.MapSynchronisationRoutes();
configuration.Routes.MapHttpRoute(
"Default",
"{controller}/{id}",
new { id = RouteParameter.Optional });
appBuilder.UseWebApi(configuration);
}
示例9: CanSetDefaultLifetimeToThreadLocalStorageLifetime
public void CanSetDefaultLifetimeToThreadLocalStorageLifetime()
{
using (var container = new IocContainer(() => new ThreadLocalLifetime()))
{
Assert.IsInstanceOfType(container.DefaultLifetimeFactory(), typeof(ThreadLocalLifetime));
}
}
示例10: ThreadLocalStorageLifetimeReturnsSameInstanceForSameThread
public void ThreadLocalStorageLifetimeReturnsSameInstanceForSameThread()
{
using (var container = new IocContainer())
{
container.Register<IFoo>(c => new Foo1()).SetLifetime(new ThreadLocalLifetime());
IFoo result1 = container.Resolve<IFoo>();
IFoo result2 = container.Resolve<IFoo>();
IFoo result3 = null;
IFoo result4 = null;
// Resolve on a different thread
var task = Task.Factory.StartNew(() =>
{
result3 = container.Resolve<IFoo>();
result4 = container.Resolve<IFoo>();
});
task.Wait();
// Assert
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.IsNotNull(result3);
Assert.IsNotNull(result4);
Assert.AreSame(result1, result2);
Assert.AreSame(result3, result4);
Assert.AreNotSame(result1, result3);
}
}
示例11: Prepare
public override void Prepare()
{
this.container = new IocContainer();
this.container.Register<ISingleton, Singleton>().WithLifetimeManager(new ContainerLifetime());
this.container.Register<ITransient, Transient>().WithLifetimeManager(new AlwaysNewLifetime());
this.container.Register<ICombined, Combined>().WithLifetimeManager(new AlwaysNewLifetime());
}
示例12: TransientLifetimeAlwaysReturnsANewInstance
public void TransientLifetimeAlwaysReturnsANewInstance()
{
// Arrange
using (var container = new IocContainer())
{
container.Register<IFoo>(c => new Foo1()); //.SetLifetime(new TransientLifetime<IFoo>);
// Act
var result1 = container.Resolve<IFoo>();
var result2 = container.Resolve<IFoo>();
var result3 = container.Resolve<IFoo>();
// Assert
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.IsNotNull(result3);
Assert.AreNotSame(result1, result2);
Assert.AreNotSame(result2, result3);
Assert.AreNotSame(result1, result3);
Assert.IsInstanceOfType(result1, typeof(Foo1));
Assert.IsInstanceOfType(result2, typeof(Foo1));
Assert.IsInstanceOfType(result3, typeof(Foo1));
}
}
示例13: TryResolveAllReturnsExpectedInstances
public void TryResolveAllReturnsExpectedInstances()
{
using (var container = new IocContainer())
{
// Arrange
var foo1 = new Foo1();
var foo2 = new Foo2();
var foo3 = new Foo2();
var bar1 = new Bar1();
container.RegisterInstance<IFoo>(foo1);
container.RegisterInstance<IFoo>(foo2, "Foo1");
container.RegisterInstance<IFoo>(foo3, "Foo2");
container.RegisterInstance<IBar>(bar1);
// Act
var results = container.TryResolveAll<IFoo>();
var resultList = results.ToList();
// Assert
Assert.IsTrue(results.Count() == 3);
CollectionAssert.Contains(resultList, foo1);
CollectionAssert.Contains(resultList, foo2);
CollectionAssert.Contains(resultList, foo3);
}
}
示例14: GenericResolveByNameNotRegisteredThrowsException2
public void GenericResolveByNameNotRegisteredThrowsException2()
{
using (var container = new IocContainer())
{
container.Register<IFoo>(c => new Foo1());
container.Resolve<IFoo>("Foo");
}
}
示例15: Application_Start
protected void Application_Start()
{
IocContainer = new IocContainer();
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}