本文整理汇总了C#中DependencyInjectionContainer.Configure方法的典型用法代码示例。如果您正苦于以下问题:C# DependencyInjectionContainer.Configure方法的具体用法?C# DependencyInjectionContainer.Configure怎么用?C# DependencyInjectionContainer.Configure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DependencyInjectionContainer
的用法示例。
在下文中一共展示了DependencyInjectionContainer.Configure方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BulkLocateWithKey
public void BulkLocateWithKey()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export(Types.FromThisAssembly())
.ByInterface<ISimpleObject>()
.WithKey(t => t.Name.Last()));
container.Configure(c =>
{
foreach (char ch in "ABCDE")
{
c.Export<ImportSingleSimpleObject>()
.WithKey(ch)
.WithCtorParam<ISimpleObject>()
.LocateWithKey(ch);
}
});
ImportSingleSimpleObject single = container.Locate<ImportSingleSimpleObject>(withKey: 'A');
Assert.NotNull(single);
Assert.IsType<SimpleObjectA>(single.SimpleObject);
Assert.Equal(3, container.LocateAll<ImportSingleSimpleObject>(withKey: new[] { 'A', 'C', 'E' }).Count);
}
示例2: CombinedSpecialTest
public void CombinedSpecialTest()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export<LazyService>().As<ILazyService>().WithMetadata("Hello", "World"));
LazyService.Created = false;
Lazy<Meta<ILazyService>> lazy = container.Locate<Lazy<Meta<ILazyService>>>();
Assert.NotNull(lazy);
Assert.False(LazyService.Created);
Assert.NotNull(lazy.Value);
Assert.NotNull(lazy.Value.Value);
ILazyService service = lazy.Value.Value;
Assert.NotNull(service);
Assert.True(LazyService.Created);
KeyValuePair<string, object> metadata = lazy.Value.Metadata.First();
Assert.Equal("Hello", metadata.Key);
Assert.Equal("World", metadata.Value);
}
示例3: OwnedLazyMetaTest
public void OwnedLazyMetaTest()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export<DisposableService>().As<IDisposableService>().WithMetadata(
"Hello",
"World"));
Owned<Lazy<Meta<IDisposableService>>> ownedLazy =
container.Locate<Owned<Lazy<Meta<IDisposableService>>>>();
Assert.NotNull(ownedLazy);
Assert.NotNull(ownedLazy.Value);
Assert.NotNull(ownedLazy.Value.Value);
Assert.NotNull(ownedLazy.Value.Value.Value);
bool disposedCalled = false;
ownedLazy.Value.Value.Value.Disposing += (sender, args) => disposedCalled = true;
ownedLazy.Dispose();
Assert.True(disposedCalled);
KeyValuePair<string, object> metadata = ownedLazy.Value.Value.Metadata.First();
Assert.Equal("Hello", metadata.Key);
Assert.Equal("World", metadata.Value);
}
示例4: Main
static int Main(string[] args)
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export(Types.FromThisAssembly()).
ByInterface(typeof(IExampleModule)).
ByInterface(typeof(IExampleSubModule<>)).
ByInterface(typeof(IExample<>)));
IEnumerable<IExampleModule> exampleModules = container.LocateAll<IExampleModule>();
try
{
foreach (IExampleModule exampleModule in exampleModules)
{
exampleModule.Execute();
}
}
catch (Exception exp)
{
Console.WriteLine("Exception thrown");
Console.WriteLine(exp.Message);
return -1;
}
return 0;
}
示例5: BeginLifetimeScope
public void BeginLifetimeScope()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export<DisposableService>().As<IDisposableService>().Lifestyle.SingletonPerScope());
IDisposableService service = container.Locate<IDisposableService>();
Assert.NotNull(service);
bool called = false;
using (var scope = container.BeginLifetimeScope())
{
var secondService = scope.Locate<IDisposableService>();
Assert.NotNull(secondService);
Assert.NotSame(service, secondService);
Assert.Same(secondService, scope.Locate<IDisposableService>());
secondService.Disposing += (sender, args) => called = true;
}
Assert.True(called);
}
示例6: FactoryFiveArgWithOutBasicAndOutOfOrderTest
public void FactoryFiveArgWithOutBasicAndOutOfOrderTest()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
BasicService basicService = new BasicService();
container.Configure(c =>
{
c.Export<FiveArgParameterService>().As<IArrayOfObjectsPropertyService>();
c.ExportInstance(basicService).As<IBasicService>();
});
FiveArgParameterService.ActivateWithOutBasicServiceAndOutOfOrder factory =
container.Locate<FiveArgParameterService.ActivateWithOutBasicServiceAndOutOfOrder>();
Assert.NotNull(factory);
IArrayOfObjectsPropertyService instance = factory(14.0m, "Blah", 9.0, 5);
Assert.NotNull(instance);
Assert.Equal(5, instance.Parameters.Length);
Assert.Equal("Blah", instance.Parameters[0]);
Assert.Equal(5, instance.Parameters[1]);
Assert.Equal(9.0, instance.Parameters[2]);
Assert.Equal(14.0m, instance.Parameters[3]);
Assert.Equal(basicService, instance.Parameters[4]);
}
示例7: AutoCreateTest
public void AutoCreateTest()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export<BasicService>().As<IBasicService>());
ImportConstructorService constructorService = container.Locate<ImportConstructorService>();
Assert.NotNull(constructorService);
}
示例8: InEnvironment
public void InEnvironment()
{
DependencyInjectionContainer container = new DependencyInjectionContainer(ExportEnvironment.UnitTest);
container.Configure(c => c.ExportAssembly(GetType().Assembly).InEnvironment(ExportEnvironment.RunTimeOnly));
IEnumerable<ISimpleObject> simpleObjects = container.LocateAll<ISimpleObject>();
Assert.NotNull(simpleObjects);
Assert.Equal(0, simpleObjects.Count());
}
示例9: CreateDIContainer
private static IDependencyInjectionContainer CreateDIContainer()
{
var container = new DependencyInjectionContainer { ThrowExceptions = false };
container.Configure(
registration => registration
.Export<PeopleService>()
.As<IPeopleService>());
return container;
}
示例10: SelectTypes
public void SelectTypes()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(
c => c.ExportAssembly(GetType().Assembly).ByInterface(typeof(ISimpleObject)).Select(TypesThat.EndWith("C")));
IEnumerable<ISimpleObject> simpleObjects = container.LocateAll<ISimpleObject>();
Assert.NotNull(simpleObjects);
Assert.Equal(1, simpleObjects.Count());
}
示例11: ExportInterfaces
public void ExportInterfaces()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(
c => c.ExportAssembly(GetType().Assembly).ByInterfaces(t => t.Name.StartsWith("ISimple")));
IEnumerable<ISimpleObject> simpleObjects = container.LocateAll<ISimpleObject>();
Assert.NotNull(simpleObjects);
Assert.Equal(5, simpleObjects.Count());
}
示例12: BasedOnInterfaceByType
public void BasedOnInterfaceByType()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export(Types.FromThisAssembly())
.BasedOn<ISimpleObject>()
.ByType());
SimpleObjectA simpleObjectA = container.Locate<SimpleObjectA>();
Assert.NotNull(simpleObjectA);
}
示例13: DebugConsoleLog
public void DebugConsoleLog()
{
Logger.SetLogService(new DebugConsoleLogService());
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export<BasicService>().As<IBasicService>());
IBasicService basicService = container.Locate<IBasicService>();
Assert.NotNull(basicService);
}
示例14: AttributedPropertyInjection
public void AttributedPropertyInjection()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export<AttributeBasicService>().As<IAttributeBasicService>());
AttributedImportPropertyService service = new AttributedImportPropertyService();
container.Inject(service);
Assert.NotNull(service.BasicService);
Assert.IsType(typeof(AttributeBasicService), service.BasicService);
}
示例15: AsKeyedStringTest
public void AsKeyedStringTest()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c =>
{
c.ExportInstance((scope, context) => "Hello");
c.ExportInstance((scope, context) => "HelloAgain").AsKeyed<string,string>("Key");
});
Assert.Equal("Hello",container.Locate<string>());
Assert.Equal("HelloAgain",container.Locate<string>(withKey: "Key"));
}