本文整理汇总了C#中DependencyInjectionContainer.CreateChildScope方法的典型用法代码示例。如果您正苦于以下问题:C# DependencyInjectionContainer.CreateChildScope方法的具体用法?C# DependencyInjectionContainer.CreateChildScope怎么用?C# DependencyInjectionContainer.CreateChildScope使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DependencyInjectionContainer
的用法示例。
在下文中一共展示了DependencyInjectionContainer.CreateChildScope方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SingletonPerNamedScopeBasicTest
public void SingletonPerNamedScopeBasicTest()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export<BasicService>().As<IBasicService>().Lifestyle.SingletonPerNamedScope("Test"));
var childScope = container.CreateChildScope(scopeName: "Test");
IBasicService basicService = childScope.Locate<IBasicService>();
Assert.NotNull(basicService);
Assert.Same(basicService, childScope.Locate<IBasicService>());
}
示例2: SingletonPerNamedScopeDisposal
public void SingletonPerNamedScopeDisposal()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export<DisposableService>().As<IDisposableService>().Lifestyle.SingletonPerNamedScope("Test"));
bool disposed = false;
using (var childScope = container.CreateChildScope(scopeName: "Test"))
{
childScope.Locate<IDisposableService>().Disposing += (sender, args) => disposed = true;
}
Assert.True(disposed);
}
示例3: ResolveFromChildScope
public void ResolveFromChildScope()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
SimpleSecondaryExportLocator resolver = new SimpleSecondaryExportLocator();
container.AddSecondaryLocator(resolver);
container.Configure(c => c.Export<ImportConstructorService>().As<IImportConstructorService>());
resolver.AddResolveValue((IBasicService)new BasicService());
IInjectionScope childScope = container.CreateChildScope();
IImportConstructorService constructorService = childScope.Locate<IImportConstructorService>();
Assert.NotNull(constructorService);
}
示例4: WhatDoIHaveTestDoNotIncludeParent
public void WhatDoIHaveTestDoNotIncludeParent()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export<ConstructorImportService>());
using (IInjectionScope child = container.CreateChildScope(c => c.Export<BasicService>().As<IBasicService>().AndSingleton()))
{
string whatDoIHave = child.WhatDoIHave();
Console.Write(whatDoIHave);
Assert.NotNull(whatDoIHave);
Assert.Contains("BasicService", whatDoIHave);
Assert.DoesNotContain("ConstructorImportService", whatDoIHave);
}
}
示例5: WhatDoIHaveFilteredTest
public void WhatDoIHaveFilteredTest()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export<ConstructorImportService>());
using (IInjectionScope child = container.CreateChildScope(c => c.Export<BasicService>().As<IBasicService>().Lifestyle.Singleton()))
{
string whatDoIHave = child.WhatDoIHave(includeParent: true, consider: ExportsThat.AreExportedAs<IBasicService>());
Console.Write(whatDoIHave);
Assert.NotNull(whatDoIHave);
Assert.Contains("BasicService", whatDoIHave);
Assert.DoesNotContain("ConstructorImportService", whatDoIHave);
}
}
示例6: PropertyInjectorInspectorChildScope
public void PropertyInjectorInspectorChildScope()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.AddStrategyInspector(new PropertyInjectionInspector<IBasicService>());
var child = container.CreateChildScope();
child.Configure(c =>
{
c.Export<BasicService>().As<IBasicService>();
c.Export<ImportPropertyService>().As<IImportPropertyService>();
});
IImportPropertyService propertyService = child.Locate<IImportPropertyService>();
Assert.NotNull(propertyService);
Assert.NotNull(propertyService.BasicService);
}
示例7: MissingExportFromChildScopeTest
public void MissingExportFromChildScopeTest()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export<ImportConstructorService>().As<IImportConstructorService>());
container.ResolveUnknownExports += (sender, args) =>
{
args.ExportedValue = new BasicService();
};
IInjectionScope childScope = container.CreateChildScope();
IImportConstructorService constructorService = childScope.Locate<IImportConstructorService>();
Assert.NotNull(constructorService);
}
示例8: SingletonPerScopeNamedDifferentNamedScopes
public void SingletonPerScopeNamedDifferentNamedScopes()
{
DependencyInjectionContainer container = new DependencyInjectionContainer();
container.Configure(c => c.Export<BasicService>().As<IBasicService>().Lifestyle.SingletonPerNamedScope("Test"));
var child1 = container.CreateChildScope(scopeName: "Test");
IBasicService baseService1 = child1.Locate<IBasicService>();
Assert.NotNull(baseService1);
Assert.Same(baseService1, child1.Locate<IBasicService>());
var child2 = container.CreateChildScope(scopeName: "Test");
IBasicService baseService2 = child2.Locate<IBasicService>();
Assert.NotNull(baseService2);
Assert.Same(baseService2, child2.Locate<IBasicService>());
Assert.NotSame(baseService1, baseService2);
}