本文整理汇总了C#中IocContainer.Resolve方法的典型用法代码示例。如果您正苦于以下问题:C# IocContainer.Resolve方法的具体用法?C# IocContainer.Resolve怎么用?C# IocContainer.Resolve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IocContainer
的用法示例。
在下文中一共展示了IocContainer.Resolve方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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));
}
}
示例2: 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);
}
}
示例3: 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);
}
}
示例4: TestRegisterSingletonClassGeneric
public void TestRegisterSingletonClassGeneric()
{
var container = new IocContainer();
container.Register<ConnectionName>(LifestyleType.Singleton);
ConnectionName connectionName = container.Resolve<ConnectionName>();
Assert.NotNull(connectionName);
ConnectionName connectionName2 = container.Resolve<ConnectionName>();
Assert.NotNull(connectionName);
Assert.Equal(connectionName, connectionName2);
}
示例5: TestRegisterSingletonClass
public void TestRegisterSingletonClass()
{
var container = new IocContainer();
container.Register(typeof(ConnectionName), LifestyleType.Singleton);
ConnectionName connectionName = (ConnectionName)container.Resolve(typeof(ConnectionName));
Assert.NotNull(connectionName);
ConnectionName connectionName2 = (ConnectionName)container.Resolve(typeof(ConnectionName));
Assert.NotNull(connectionName);
Assert.Equal(connectionName, connectionName2);
}
示例6: TestRegisterSingletonInterface
public void TestRegisterSingletonInterface()
{
var container = new IocContainer();
container.Register(typeof(ConnectionName));
container.Register(typeof(IConnection), typeof(SqlConnection), LifestyleType.Singleton);
IConnection connection = (IConnection)container.Resolve(typeof(IConnection));
Assert.NotNull(connection);
IConnection connection2 = (IConnection)container.Resolve(typeof(IConnection));
Assert.NotNull(connection);
Assert.Equal(connection, connection2);
}
示例7: GenericResolveByTypeNotRegisteredThrowsException
public void GenericResolveByTypeNotRegisteredThrowsException()
{
using (var container = new IocContainer())
{
container.Resolve<IFoo>();
}
}
示例8: CanRegisterAndResolveMultipleTypes
public void CanRegisterAndResolveMultipleTypes()
{
using (var container = new IocContainer())
{
container.Register<IFoo>(c => new Foo1());
container.Register<IBar>(c => new Bar1());
var result1 = container.Resolve<IFoo>();
var result2 = container.Resolve<IBar>();
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.IsInstanceOfType(result1, typeof(Foo1));
Assert.IsInstanceOfType(result2, typeof(Bar1));
}
}
示例9: ContainerLifetimeManagerExtensionAlwaysReturnsSameInstance
public void ContainerLifetimeManagerExtensionAlwaysReturnsSameInstance()
{
using (var iocContainer = new IocContainer())
{
iocContainer.Register<IFoo>(c => new Foo1()).AsContainerSingleton();
var result1 = iocContainer.Resolve<IFoo>();
var result2 = iocContainer.Resolve<IFoo>();
var result3 = iocContainer.Resolve<IFoo>();
Verify.That(result3).IsNotNull();
Verify.That(result2).IsNotNull();
Verify.That(result1).IsNotNull()
.IsTheSameObjectAs(result2)
.IsTheSameObjectAs(result3);
}
}
示例10: GenericResolveByNameNotRegisteredThrowsException2
public void GenericResolveByNameNotRegisteredThrowsException2()
{
using (var container = new IocContainer())
{
container.Register<IFoo>(c => new Foo1());
container.Resolve<IFoo>("Foo");
}
}
示例11: RequestLifetimeManagerExtensionReturnsSameObjectForSameRequest
public void RequestLifetimeManagerExtensionReturnsSameObjectForSameRequest()
{
var context1 = new FakeHttpContext("Http://fakeUrl1.com");
var context2 = new FakeHttpContext("Http://fakeUrl2.com");
using (var container = new IocContainer())
{
container.Register<IFoo>(c => new Foo1()).AsRequestSingleton();
LifetimeExtensions.HttpContext = context1;
var result1 = container.Resolve<IFoo>();
var result2 = container.Resolve<IFoo>();
LifetimeExtensions.HttpContext = context2;
var result3 = container.Resolve<IFoo>();
Verify.That(result3).IsNotNull();
Verify.That(result2).IsNotNull();
Verify.That(result1).IsNotNull().IsTheSameObjectAs(result2).IsNotTheSameObjectAs(result3);
}
}
示例12: DefaultLifetimeManagerAlwaysReturnsNewInstance
public void DefaultLifetimeManagerAlwaysReturnsNewInstance()
{
using (var iocContainer = new IocContainer())
{
iocContainer.Register<IFoo>(c => new Foo1());
var result1 = iocContainer.Resolve<IFoo>();
var result2 = iocContainer.Resolve<IFoo>();
var result3 = iocContainer.Resolve<IFoo>();
Verify.That(result3).IsNotNull();
Verify.That(result2).IsNotNull()
.IsNotTheSameObjectAs(result3);
Verify.That(result1).IsNotNull()
.IsNotTheSameObjectAs(result2)
.IsNotTheSameObjectAs(result3);
}
}
示例13: CanChangeLifetimeUsingSetLifetime
public void CanChangeLifetimeUsingSetLifetime()
{
// Move to somewhere ...
// Isnt it already in the Register Tests ?
using (var container = new IocContainer(() => new TransientLifetime()))
{
Assert.IsInstanceOfType(container.DefaultLifetime, typeof(TransientLifetime));
var registration = container.Register<IFoo>(c => new Foo1()).SetLifetime(new ContainerLifetime());
Assert.IsInstanceOfType(registration.Lifetime, typeof(ContainerLifetime));
var foo1 = container.Resolve<IFoo>();
var foo2 = container.Resolve<IFoo>();
Assert.AreSame(foo1, foo2);
}
}
示例14: ContainerLifetimeAlwaysReturnsSameInstance
public void ContainerLifetimeAlwaysReturnsSameInstance()
{
using (var container = new IocContainer(() => new ContainerLifetime()))
{
container.Register<IFoo>(c => new Foo1());
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.AreSame(result1, result2);
Assert.AreSame(result2, result3);
Assert.AreSame(result1, result3);
}
}
示例15: AlwayNewLifetimeManagerExtensionAlwaysReturnsNewInstance
public void AlwayNewLifetimeManagerExtensionAlwaysReturnsNewInstance()
{
using (var iocContainer = new IocContainer())
{
iocContainer.Register<IFoo>(c => new Foo1()).AsAlwaysNew();
var foo1 = iocContainer.Resolve<IFoo>();
var foo2 = iocContainer.Resolve<IFoo>();
var foo3 = iocContainer.Resolve<IFoo>();
Verify.That(foo1).IsAnInstanceOfType(typeof(IFoo))
.IsNotTheSameObjectAs(foo2)
.IsNotTheSameObjectAs(foo3);
Verify.That(foo2).IsAnInstanceOfType(typeof(IFoo))
.IsNotTheSameObjectAs(foo3);
Verify.That(foo3).IsAnInstanceOfType(typeof(IFoo));
}
}