Call BuilderContext by reference

This commit is contained in:
Eugene Sadovoi 2018-09-29 17:22:52 -04:00
Родитель 5867856b76
Коммит 406d617041
2 изменённых файлов: 14 добавлений и 9 удалений

Просмотреть файл

@ -7,7 +7,8 @@ namespace BuildPlanExample
{
public class FooBuildPlanPolicy : IBuildPlanPolicy
{
public void BuildUp(IBuilderContext context)
public void BuildUp<TBuilderContext>(ref TBuilderContext context)
where TBuilderContext : IBuilderContext
{
// Resolve requested type
var argument = context.BuildKey.Type.GetTypeInfo().GenericTypeArguments[0];

Просмотреть файл

@ -1,5 +1,7 @@
using System.Reflection;
using System.Linq;
using System.Reflection;
using Unity.Builder;
using Unity.Delegates;
using Unity.ObjectBuilder.BuildPlan.DynamicMethod;
using Unity.Policy;
@ -21,15 +23,16 @@ namespace BuildPlanCreatorExample
_policies = policies;
}
public IBuildPlanPolicy CreatePlan(IBuilderContext context, INamedType buildKey)
public IBuildPlanPolicy CreatePlan<TBuilderContext>(ref TBuilderContext context, INamedType buildKey)
where TBuilderContext : IBuilderContext
{
// Make generic factory method for the type
var typeToBuild = buildKey.Type.GetTypeInfo().GenericTypeArguments;
var typeToBuild = buildKey.Type.GetTypeInfo().GenericTypeArguments.First();
var factoryMethod =
_factoryMethod.MakeGenericMethod(typeToBuild)
.CreateDelegate(typeof(DynamicBuildPlanMethod));
_factoryMethod.MakeGenericMethod(typeof(TBuilderContext), typeToBuild)
.CreateDelegate(typeof(ResolveDelegate<TBuilderContext>));
// Create policy
var creatorPlan = new DynamicMethodBuildPlan((DynamicBuildPlanMethod)factoryMethod);
var creatorPlan = new DynamicMethodBuildPlan(factoryMethod);
// Register BuildPlan policy with the container to optimize performance
_policies.Set(buildKey.Type, string.Empty, typeof(IBuildPlanPolicy), creatorPlan);
@ -37,13 +40,14 @@ namespace BuildPlanCreatorExample
return creatorPlan;
}
private static void FactoryMethod<TResult>(IBuilderContext context)
private static object FactoryMethod<TBuilderContext, TResult>(ref TBuilderContext context)
where TBuilderContext : IBuilderContext
{
// Resolve requested type
var service = (TResult)context.Container.Resolve(typeof(TResult), context.BuildKey.Name);
// Create Foo
context.Existing = new Foo<TResult>(service);
return new Foo<TResult>(service);
}
}
}