diff --git a/src/Extending Unity/BuildPlan/FooBuildPlanPolicy.cs b/src/Extending Unity/BuildPlan/FooBuildPlanPolicy.cs index 1a4b615..e74db02 100644 --- a/src/Extending Unity/BuildPlan/FooBuildPlanPolicy.cs +++ b/src/Extending Unity/BuildPlan/FooBuildPlanPolicy.cs @@ -5,21 +5,18 @@ using Unity.Policy; namespace BuildPlanExample { - public class FooBuildPlanPolicy : IBuildPlanPolicy + public static class FooBuildPlanPolicy { - public void BuildUp(ref BuilderContext context) + + public static ResolveDelegate GetResolver(ref BuilderContext context) { // Resolve requested type var argument = context.Type.GetTypeInfo().GenericTypeArguments[0]; - var service = context.Resolve(argument, context.Name); - + var name = context.Name; + var typeToBuild = typeof(Foo<>).GetTypeInfo().MakeGenericType(argument); // Create Foo - var typeToBuild = typeof(Foo<>).GetTypeInfo().MakeGenericType(argument); - context.Existing = Activator.CreateInstance(typeToBuild, service); - - // Note: This example does not optimize implementation in any way for - // simplicity. + return (ref BuilderContext c) => Activator.CreateInstance(typeToBuild, c.Resolve(argument, name)); } } } diff --git a/src/Extending Unity/BuildPlan/FooUnityExtension.cs b/src/Extending Unity/BuildPlan/FooUnityExtension.cs index a6cb7cf..6818924 100644 --- a/src/Extending Unity/BuildPlan/FooUnityExtension.cs +++ b/src/Extending Unity/BuildPlan/FooUnityExtension.cs @@ -12,8 +12,8 @@ namespace BuildPlanExample // Note name of the registration! It tells Unity that this policy // applies to ALL resolutions of the type regardless of requested name. // In other words it creates 'Built-In' registration similar to Lazy or IEnumerable. - Context.Policies.Set(typeof(IFoo<>), string.Empty, typeof(IBuildPlanPolicy), new FooBuildPlanPolicy()); - Context.Policies.Set(typeof(Foo<>), string.Empty, typeof(IBuildPlanPolicy), new FooBuildPlanPolicy()); // Optional + Context.Policies.Set(typeof(IFoo<>), string.Empty, typeof(ResolveDelegateFactory), (ResolveDelegateFactory)FooBuildPlanPolicy.GetResolver); + Context.Policies.Set(typeof(Foo<>), string.Empty, typeof(ResolveDelegateFactory), (ResolveDelegateFactory)FooBuildPlanPolicy.GetResolver); // Optional } } }