本文整理汇总了C#中System.IocContainer类的典型用法代码示例。如果您正苦于以下问题:C# IocContainer类的具体用法?C# IocContainer怎么用?C# IocContainer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IocContainer类属于System命名空间,在下文中一共展示了IocContainer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ContainerCanBeCreated
public void ContainerCanBeCreated()
{
using (var container = new IocContainer())
{
Assert.IsNotNull(container);
}
}
示例2: CallRegistrarsInDirectory
/// <summary>
/// Finds all the types that implement the IMunqConfig interface, create an instance and
/// then call the RegisterIn method on the type.
/// </summary>
/// <param name="container">The Munq IOC container to register the class factories in.</param>
/// <param name="binPath">The path of the directory to search.</param>
/// <param name="filePattern">The optional file pattern for files to check. The default is *.dll</param>
public static void CallRegistrarsInDirectory(IocContainer container, string binPath, string filePattern = "*.dll")
{
var assemblyNames = Directory.GetFiles(binPath, filePattern);
foreach (var filename in assemblyNames)
CallRegistrarsInAssembly(container, filename);
}
示例3: FindAndRegisterDependencies
/// <summary>
/// Finds all the types that implement the IMunqConfig interface, create an instance and
/// then call the RegisterIn method on the type. The bin directory is checked for web
/// applications, the current directory for Windows applications.
/// </summary>
/// <param name="container">The Munq IOC container to register class factories in.</param>
public static void FindAndRegisterDependencies(IocContainer container)
{
// get all the assemblies in the bin directory
string binPath = HttpContext.Current != null ? HttpContext.Current.Server.MapPath("/bin")
: Environment.CurrentDirectory;
CallRegistrarsInDirectory(container, binPath);
}
示例4: OnNavigatedTo
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
var ioc = new IocContainer();
CustomRegistrations(ioc);
AutoRegistrations(ioc);
var messages = new List<string>
{
ioc.Resolve<ISomething>().Say(),
ioc.Resolve<Something>().Say(),
ioc.Resolve<ISomethingOrOther>().Say(),
ioc.Resolve<IInterfaceForBaseType>().Say(),
ioc.Resolve<IInterfaceForDerivedType>().Say(),
ioc.Resolve<ICustomInterface>().Say()
};
try
{
ioc.Resolve<Object>();
}
catch (TypeNotRegisteredException ex)
{
messages.Add(ex.Message);
}
var dialogContent = string.Empty;
messages.ForEach(m => dialogContent += m + Environment.NewLine);
await new MessageDialog(dialogContent).ShowAsync();
Application.Current.Exit();
}
示例5: ContainerScopeTryResolveWorks
public void ContainerScopeTryResolveWorks()
{
using (var container = new IocContainer())
{
// Register
container.Register<IFoo, Foo1>();
container.Register<IFoo, Foo2>("Key1");
container.Register<IBar, Bar1>();
using (var scope = container.GetScope())
{
IFoo instance1, instance2;
var result1 = scope.TryResolve<IFoo>(out instance1);
var result2 = scope.TryResolve<IFoo>(out instance2);
Assert.AreSame(instance1, instance2);
IFooBar instance3, instance4;
var result3 = scope.TryResolve<IFooBar>(out instance3);
var result4 = scope.TryResolve<IFooBar>(out instance4);
Assert.AreSame(instance3, instance4);
}
}
}
示例6: CachedLifetimeCanBeSetAsDefaultLifetime
public void CachedLifetimeCanBeSetAsDefaultLifetime()
{
using (var container = new IocContainer(() => new CachedLifetime()))
{
Assert.IsInstanceOfType(container.DefaultLifetimeFactory(), typeof(CachedLifetime));
}
}
示例7: CachedLifetimeReturnsDifferentInstanceIfCacheExpired
public void CachedLifetimeReturnsDifferentInstanceIfCacheExpired()
{
using (var container = new IocContainer())
{
var policy = new CacheItemPolicy()
{
SlidingExpiration = new TimeSpan(0, 0, 1)
};
container.Register<IFoo>(c => new Foo1()).WithCachedLifetime(policy);
var result1 = container.Resolve<IFoo>();
var result2 = container.Resolve<IFoo>();
Thread.Sleep(1500);
var result3 = container.Resolve<IFoo>();
// Assert
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.IsNotNull(result3);
Assert.AreSame(result1, result2);
Assert.AreNotSame(result1, result3);
}
}
示例8: Broadcasters
public Broadcasters(XDMessagingClient messagingClient, IocContainer container)
: base(container)
{
Validate.That(messagingClient).IsNotNull();
this.messagingClient = messagingClient;
}
示例9: Main
static void Main(string[] args)
{
var container = new IocContainer();
container.RegisterFromAssemblyOf<Program>();
Configure.Messaging()
.LoggingWith(new ConsoleLoggingMechanism { ShowDebug = false })
.UsingFilePersistence()
.ResolveReferencesWith(container)
.RegisterHandlersFromContainer().BasedOn<IMessageHandler>()
.UsingHttpTransport()
.AsAServer("ReceiverServer")
.RequiresAuthentication()
.AcceptsRequest<AuthenticationRequest>()
.AuthenticatesOnReply<AuthenticatedResponse>()
.ExpiresAfter(TimeSpan.FromMinutes(1))
.OnExpiry(s => Console.WriteLine("Session expired: {0}", s))
.OpenChannel("TestReply")
.ForRequestReplyReceiving()
.WithDurability()
.Initialise();
Console.WriteLine("I am the reciever. Press enter to exit");
Console.ReadLine();
}
示例10: ServiceBuilder
public ServiceBuilder(IocContainer container)
{
if(container == null) throw new ArgumentNullException(nameof(container));
this._container = container;
this.TypeBinders = new Lazy<List<TypeServiceBinder>>();
this.ValueBinders = new Lazy<List<ValueServiceBinder>>();
}
示例11: Set_Test
public void Set_Test()
{
var locator = new IocContainer();
using(var builder = new ServiceBuilder(locator))
{
var binder = builder.Use(Types.String);
Assert.IsType<TypeServiceBinder>(binder);
Assert.True(binder.Overwrite);
Assert.Equal(Types.String, binder.ExpectType);
Assert.Equal(binder, builder.TypeBinders.Value[0]);
var binder2 = builder.Use("abc");
Assert.IsType<ValueServiceBinder>(binder2);
Assert.Equal("abc", binder2.Name);
Assert.Null(binder2.ExpectType);
Assert.Equal(binder2, builder.ValueBinders.Value[0]);
var binder3 = builder.Use(Types.String, "abc");
Assert.IsType<ValueServiceBinder>(binder3);
Assert.Equal("abc", binder3.Name);
Assert.Equal(Types.String, binder3.ExpectType);
Assert.Equal(binder3, builder.ValueBinders.Value[1]);
}
}
示例12: ExpressionRegistrationCanBeChanged
public void ExpressionRegistrationCanBeChanged()
{
var container = new IocContainer();
var lifetime = new TransientLifetime();
var reg = new ExpressionRegistration<IFoo>(container, x => new Foo1(), lifetime, CompileMode.Delegate);
Assert.AreSame(reg.Lifetime, lifetime);
Assert.AreEqual(reg.CompileMode, CompileMode.Delegate);
var get1 = reg.GetInstance();
var newLifetime = new ContainerLifetime();
var newCompileMode = CompileMode.Dynamic;
// Set new lifetime
reg.SetLifetime(newLifetime);
Assert.AreSame(reg.Lifetime, newLifetime);
// Set different compile mode
reg.SetCompileMode(newCompileMode);
Assert.AreEqual(reg.CompileMode, newCompileMode);
var get2 = reg.GetInstance();
var get3 = reg.GetInstance();
// Check that the lifetime is also being used
Assert.AreNotSame(get1, get2);
Assert.AreSame(get2, get3);
}
示例13: Listeners
public Listeners(XDMessagingClient client, IocContainer container)
: base(container)
{
Validate.That(client).IsNotNull();
this.client = client;
}
示例14: IndexReturnsEmptyEnumerableWhenEmpty
public void IndexReturnsEmptyEnumerableWhenEmpty()
{
using (var container = new IocContainer())
{
Assert.IsFalse(container.Index.Any());
}
}
示例15: 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>("Foo1", foo2);
container.RegisterInstance<IFoo>("Foo2", foo3);
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);
}
}