Deprecated old factory interfaces

This commit is contained in:
Eugene Sadovoi 2018-12-28 23:23:14 -05:00
Родитель 645b43b87f
Коммит b3a93a56ab
2 изменённых файлов: 8 добавлений и 11 удалений

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

@ -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<BuilderContext> 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));
}
}
}

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

@ -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
}
}
}