本文整理汇总了C#中IContainerAdapter.CreateChildContainerAdapter方法的典型用法代码示例。如果您正苦于以下问题:C# IContainerAdapter.CreateChildContainerAdapter方法的具体用法?C# IContainerAdapter.CreateChildContainerAdapter怎么用?C# IContainerAdapter.CreateChildContainerAdapter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IContainerAdapter
的用法示例。
在下文中一共展示了IContainerAdapter.CreateChildContainerAdapter方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MethodToBenchmark
public override void MethodToBenchmark(IContainerAdapter container)
{
using (var childContainer = container.CreateChildContainerAdapter())
{
childContainer.Prepare();
var scopedCombined = (ICombined1)childContainer.Resolve(typeof(ICombined1));
}
using (var childContainer = container.CreateChildContainerAdapter())
{
childContainer.Prepare();
var scopedCombined = (ICombined2)childContainer.Resolve(typeof(ICombined2));
}
using (var childContainer = container.CreateChildContainerAdapter())
{
childContainer.Prepare();
var scopedCombined = (ICombined3)childContainer.Resolve(typeof(ICombined3));
}
}
示例2: WarmUp
//.........这里部分代码省略.........
if (complex == null)
{
throw new Exception(string.Format("Container {0} could not create type {1}", container.PackageName, typeof(IComplex)));
}
if (container.SupportsPropertyInjection)
{
var propertyInjectionObject = (ComplexPropertyObject)container.Resolve(typeof(IComplexPropertyObject));
var propertyInjectionObject2 = (ComplexPropertyObject)container.Resolve(typeof(IComplexPropertyObject));
if (propertyInjectionObject == null || propertyInjectionObject2 == null)
{
throw new Exception(
string.Format(
"Container {0} could not create type {1}",
container.PackageName,
typeof(IComplexPropertyObject)));
}
if (object.ReferenceEquals(propertyInjectionObject, propertyInjectionObject2) ||
!object.ReferenceEquals(propertyInjectionObject.ServiceA, propertyInjectionObject2.ServiceA) ||
!object.ReferenceEquals(propertyInjectionObject.ServiceB, propertyInjectionObject2.ServiceB) ||
!object.ReferenceEquals(propertyInjectionObject.ServiceC, propertyInjectionObject2.ServiceC) ||
object.ReferenceEquals(propertyInjectionObject.SubObjectA, propertyInjectionObject2.SubObjectA) ||
object.ReferenceEquals(propertyInjectionObject.SubObjectB, propertyInjectionObject2.SubObjectB) ||
object.ReferenceEquals(propertyInjectionObject.SubObjectC, propertyInjectionObject2.SubObjectC))
{
throw new Exception(
string.Format(
"Container {0} could not correctly create type {1}",
container.PackageName,
typeof(IComplexPropertyObject)));
}
propertyInjectionObject.Verify(container.PackageName);
}
if (container.SupportGeneric)
{
var generic = (ImportGeneric<int>)container.Resolve(typeof(ImportGeneric<int>));
if (generic == null)
{
throw new Exception(string.Format("Container {0} could not create type {1}", container.PackageName, typeof(ImportGeneric<int>)));
}
}
if (container.SupportsMultiple)
{
var importMultiple = (ImportMultiple)container.Resolve(typeof(ImportMultiple));
if (importMultiple == null)
{
throw new Exception(string.Format("Container {0} could not create type {1}", container.PackageName, typeof(ImportMultiple)));
}
}
if (container.SupportsConditional)
{
var importObject = (ImportConditionObject)container.Resolve(typeof(ImportConditionObject));
if (importObject == null)
{
throw new Exception(string.Format("Container {0} could not create type {1}", container.PackageName, typeof(ImportConditionObject)));
}
var importObject2 = (ImportConditionObject2)container.Resolve(typeof(ImportConditionObject2));
if (importObject2 == null)
{
throw new Exception(string.Format("Container {0} could not create type {1}", container.PackageName, typeof(ImportConditionObject2)));
}
}
if (container.SupportsInterception)
{
var calculator = (ICalculator)container.ResolveProxy(typeof(ICalculator));
calculator.Add(1, 2);
}
if (container.SupportsChildContainer)
{
using (var childContainer = container.CreateChildContainerAdapter())
{
childContainer.Prepare();
ICombined scopedCombined = (ICombined)childContainer.Resolve(typeof(ICombined));
if (scopedCombined == null)
{
throw new Exception(string.Format("Child Container {0} could not create type {1}", container.PackageName, typeof(ICombined)));
}
if (!(scopedCombined is ScopedCombined))
{
throw new Exception(string.Format("Child Container {0} resolved type incorrectly should have been {1} but was {2}", container.PackageName, typeof(ICombined), scopedCombined.GetType()));
}
}
}
}
示例3: MeasureResolvePerformance
private static void MeasureResolvePerformance(IContainerAdapter container, Result outputResult)
{
var singletonWatch = new Stopwatch();
var transientWatch = new Stopwatch();
var combinedWatch = new Stopwatch();
var conditionsWatch = new Stopwatch();
var genericWatch = new Stopwatch();
var complexWatch = new Stopwatch();
var multipleWatch = new Stopwatch();
var propertyWatch = new Stopwatch();
var childContainerWatch = new Stopwatch();
for (int i = 0; i < LoopCount; i++)
{
singletonWatch.Start();
var result1 = (ISingleton)container.Resolve(typeof(ISingleton));
singletonWatch.Stop();
transientWatch.Start();
var result2 = (ITransient)container.Resolve(typeof(ITransient));
transientWatch.Stop();
combinedWatch.Start();
var result3 = (ICombined)container.Resolve(typeof(ICombined));
combinedWatch.Stop();
complexWatch.Start();
var complexResult = (IComplex)container.Resolve(typeof(IComplex));
complexWatch.Stop();
if (container.SupportsPropertyInjection)
{
propertyWatch.Start();
var propertyInjectionResult = (IComplexPropertyObject)container.Resolve(typeof(IComplexPropertyObject));
propertyWatch.Stop();
}
if (container.SupportsConditional)
{
conditionsWatch.Start();
var result4 = (ImportConditionObject)container.Resolve(typeof(ImportConditionObject));
var result5 = (ImportConditionObject2)container.Resolve(typeof(ImportConditionObject2));
conditionsWatch.Stop();
}
if (container.SupportGeneric)
{
genericWatch.Start();
var genericResult = (ImportGeneric<int>)container.Resolve(typeof(ImportGeneric<int>));
genericWatch.Stop();
}
if (container.SupportsMultiple)
{
multipleWatch.Start();
var importMultiple = (ImportMultiple)container.Resolve(typeof(ImportMultiple));
multipleWatch.Stop();
}
if (container.SupportsChildContainer && i < ChildContainerLoopCount)
{
// Note I'm writing the test this way specifically because it matches the Per Request bootstrapper for Nance
// It's also a very common pattern used in MVC applications where a scope is created per request
childContainerWatch.Start();
using (var childContainer = container.CreateChildContainerAdapter())
{
childContainer.Prepare();
var scopedCombined = childContainer.Resolve(typeof(ICombined));
}
childContainerWatch.Stop();
}
}
outputResult.SingletonTime = singletonWatch.ElapsedMilliseconds;
outputResult.TransientTime = transientWatch.ElapsedMilliseconds;
outputResult.CombinedTime = combinedWatch.ElapsedMilliseconds;
outputResult.ComplexTime = complexWatch.ElapsedMilliseconds;
if (container.SupportsPropertyInjection)
{
outputResult.PropertyInjectionTime = propertyWatch.ElapsedMilliseconds;
}
if (container.SupportGeneric)
{
outputResult.GenericTime = genericWatch.ElapsedMilliseconds;
}
if (container.SupportsMultiple)
{
outputResult.MultipleImport = multipleWatch.ElapsedMilliseconds;
}
if (container.SupportsConditional)
{
outputResult.ConditionalTime = conditionsWatch.ElapsedMilliseconds;
}
//.........这里部分代码省略.........