Refactoring
This commit is contained in:
Родитель
af3b4963df
Коммит
5f59242dab
|
@ -2,9 +2,7 @@
|
|||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Unity.Builder;
|
||||
using Unity.Registration;
|
||||
using Unity.Resolution;
|
||||
using Unity.Storage;
|
||||
|
||||
namespace Unity.Factories
|
||||
{
|
||||
|
@ -24,10 +22,10 @@ namespace Unity.Factories
|
|||
|
||||
#region TypeResolverFactory
|
||||
|
||||
public static TypeFactoryDelegate Factory = (Type type, IRegistration? policies) =>
|
||||
public static TypeFactoryDelegate Factory = (Type type, UnityContainer container) =>
|
||||
{
|
||||
var typeArgument = type.GetElementType();
|
||||
var targetType = policies?.Owner.GetTargetType(typeArgument);
|
||||
var targetType = container.GetTargetType(typeArgument);
|
||||
|
||||
if (null != targetType && typeArgument != targetType)
|
||||
{
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Unity.Builder;
|
||||
using Unity.Registration;
|
||||
using Unity.Resolution;
|
||||
|
||||
namespace Unity.Factories
|
||||
|
@ -24,7 +23,7 @@ namespace Unity.Factories
|
|||
|
||||
#region TypeResolverFactory
|
||||
|
||||
public static TypeFactoryDelegate Factory = (Type type, IRegistration? policies) =>
|
||||
public static TypeFactoryDelegate Factory = (Type type, UnityContainer container) =>
|
||||
{
|
||||
#if NETSTANDARD1_0 || NETCOREAPP1_0 || NET40
|
||||
var typeArgument = type.GetTypeInfo().GenericTypeArguments.First();
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
using System;
|
||||
using System.Reflection;
|
||||
using Unity.Builder;
|
||||
using Unity.Registration;
|
||||
using Unity.Resolution;
|
||||
|
||||
namespace Unity.Factories
|
||||
|
@ -18,7 +17,7 @@ namespace Unity.Factories
|
|||
|
||||
#region TypeResolverFactory
|
||||
|
||||
public static TypeFactoryDelegate Factory = (Type type, IRegistration? policies) =>
|
||||
public static TypeFactoryDelegate Factory = (Type type, UnityContainer container) =>
|
||||
{
|
||||
var typeToBuild = type.GetTypeInfo().GenericTypeArguments[0];
|
||||
var factoryMethod = ImplementationMethod.MakeGenericMethod(typeToBuild);
|
||||
|
|
|
@ -25,7 +25,7 @@ namespace Unity.Factories
|
|||
|
||||
#region TypeResolverFactory
|
||||
|
||||
public static TypeFactoryDelegate Factory = (Type type, IRegistration? policies) =>
|
||||
public static TypeFactoryDelegate Factory = (Type type, UnityContainer container) =>
|
||||
{
|
||||
var itemType = type.GetTypeInfo().GenericTypeArguments[0];
|
||||
var lazyMethod = ImplementationMethod.MakeGenericMethod(itemType);
|
||||
|
|
|
@ -28,7 +28,7 @@ namespace Unity.Factories
|
|||
|
||||
#region ResolveDelegateFactory
|
||||
|
||||
public static TypeFactoryDelegate Factory = (Type type, IRegistration? policies) =>
|
||||
public static TypeFactoryDelegate Factory = (Type type, UnityContainer container) =>
|
||||
{
|
||||
|
||||
#if NETSTANDARD1_0 || NETCOREAPP1_0 || NET40
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
using System;
|
||||
using Unity.Builder;
|
||||
using Unity.Registration;
|
||||
using Unity.Resolution;
|
||||
|
||||
namespace Unity
|
||||
{
|
||||
public delegate ResolveDelegate<BuilderContext> TypeFactoryDelegate(Type type, IRegistration? policies);
|
||||
public delegate ResolveDelegate<BuilderContext> TypeFactoryDelegate(Type type, UnityContainer container);
|
||||
}
|
||||
|
|
|
@ -1,8 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using Unity.Builder;
|
||||
using Unity.Registration;
|
||||
using Unity.Builder;
|
||||
using Unity.Resolution;
|
||||
|
||||
namespace Unity
|
||||
|
@ -11,9 +7,6 @@ namespace Unity
|
|||
{
|
||||
#region Public Members
|
||||
|
||||
public virtual IEnumerable<Expression> Build(UnityContainer container, IEnumerator<Pipeline> enumerator,
|
||||
Type type, IRegistration registration) => throw new NotImplementedException();
|
||||
|
||||
public virtual ResolveDelegate<BuilderContext>? Build(ref PipelineBuilder builder) => builder.Pipeline();
|
||||
|
||||
#endregion
|
||||
|
|
|
@ -39,7 +39,7 @@ namespace Unity
|
|||
LifetimeManager = registration.LifetimeManager;
|
||||
InjectionMembers = registration.InjectionMembers;
|
||||
|
||||
Registration = registration;
|
||||
Policies = registration;
|
||||
ContainerContext = container.Context;
|
||||
|
||||
Seed = registration.Pipeline;
|
||||
|
@ -56,14 +56,14 @@ namespace Unity
|
|||
LifetimeManager = null;
|
||||
InjectionMembers = null;
|
||||
|
||||
Registration = null;
|
||||
Policies = null;
|
||||
ContainerContext = container.Context;
|
||||
|
||||
Seed = null;
|
||||
_enumerator = pipelines.GetEnumerator();
|
||||
}
|
||||
|
||||
public PipelineBuilder(Type type, string? name, UnityContainer container, IRegistration registration)
|
||||
public PipelineBuilder(Type type, string? name, UnityContainer container, ExplicitRegistration registration)
|
||||
{
|
||||
Type = type;
|
||||
Name = name;
|
||||
|
@ -72,7 +72,7 @@ namespace Unity
|
|||
LifetimeManager = registration.LifetimeManager;
|
||||
InjectionMembers = registration.InjectionMembers;
|
||||
|
||||
Registration = registration;
|
||||
Policies = registration;
|
||||
ContainerContext = container.Context;
|
||||
|
||||
Seed = registration.Pipeline;
|
||||
|
@ -89,7 +89,7 @@ namespace Unity
|
|||
LifetimeManager = context.Registration?.LifetimeManager;
|
||||
InjectionMembers = context.Registration?.InjectionMembers;
|
||||
|
||||
Registration = context.Registration;
|
||||
Policies = context.Registration;
|
||||
ContainerContext = context.ContainerContext;
|
||||
|
||||
Seed = context.Registration?.Pipeline;
|
||||
|
@ -114,13 +114,11 @@ namespace Unity
|
|||
|
||||
public Converter<Type, Type>? BuildType { get; }
|
||||
|
||||
|
||||
|
||||
public ResolveDelegate<BuilderContext>? Seed { get; private set; }
|
||||
public IPolicySet? Policies { get; }
|
||||
|
||||
public readonly ContainerContext ContainerContext;
|
||||
|
||||
public IRegistration? Registration { get; }
|
||||
public ResolveDelegate<BuilderContext>? Seed { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using Unity.Builder;
|
||||
using Unity.Lifetime;
|
||||
using Unity.Registration;
|
||||
|
||||
namespace Unity
|
||||
{
|
||||
|
@ -33,56 +31,6 @@ namespace Unity
|
|||
#endregion
|
||||
|
||||
|
||||
#region PipelineBuilder
|
||||
|
||||
public override IEnumerable<Expression> Build(UnityContainer container, IEnumerator<Pipeline> enumerator,
|
||||
Type type, IRegistration registration)
|
||||
{
|
||||
//// Select ConstructorInfo
|
||||
//var selector = GetOrDefault(registration);
|
||||
//var selection = selector.Select(type, registration)
|
||||
// .FirstOrDefault();
|
||||
|
||||
//// Select constructor for the Type
|
||||
//object[]? resolvers = null;
|
||||
//ConstructorInfo? info = null;
|
||||
//IEnumerable<Expression> parametersExpr;
|
||||
|
||||
//switch (selection)
|
||||
//{
|
||||
// case ConstructorInfo memberInfo:
|
||||
// info = memberInfo;
|
||||
// parametersExpr = CreateParameterExpressions(info.GetParameters());
|
||||
// break;
|
||||
|
||||
// case MethodBase<ConstructorInfo> injectionMember:
|
||||
// info = injectionMember.MemberInfo(type);
|
||||
// resolvers = injectionMember.Data;
|
||||
// parametersExpr = CreateParameterExpressions(info.GetParameters(), resolvers);
|
||||
// break;
|
||||
|
||||
// case Exception exception:
|
||||
// return new[] {Expression.IfThen(
|
||||
// Expression.Equal(Expression.Constant(null), BuilderContextExpression.Existing),
|
||||
// Expression.Throw(Expression.Constant(exception)))};
|
||||
|
||||
// default:
|
||||
// return NoConstructorExpr;
|
||||
//}
|
||||
|
||||
//// Get lifetime manager
|
||||
//var lifetimeManager = (LifetimeManager?)registration.Get(typeof(LifetimeManager));
|
||||
|
||||
//return lifetimeManager is PerResolveLifetimeManager
|
||||
// ? new[] { GetResolverExpression(info, resolvers), SetPerBuildSingletonExpr }
|
||||
// : new Expression[] { GetResolverExpression(info, resolvers) };
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Overrides
|
||||
|
||||
protected override Expression GetResolverExpression(ConstructorInfo info, object? resolvers)
|
||||
|
|
|
@ -39,7 +39,7 @@ namespace Unity
|
|||
}
|
||||
|
||||
// Select ConstructorInfo
|
||||
var selector = GetOrDefault(builder.Registration);
|
||||
var selector = GetOrDefault(builder.Policies);
|
||||
var selection = selector.Invoke(builder.Type, builder.InjectionMembers)
|
||||
.FirstOrDefault();
|
||||
|
||||
|
@ -77,7 +77,7 @@ namespace Unity
|
|||
};
|
||||
}
|
||||
|
||||
var lifetimeManager = (LifetimeManager?)builder.Registration?.Get(typeof(LifetimeManager));
|
||||
var lifetimeManager = (LifetimeManager?)builder.Policies?.Get(typeof(LifetimeManager));
|
||||
|
||||
return lifetimeManager is PerResolveLifetimeManager
|
||||
? GetPerResolveDelegate(info, resolvers, pipeline)
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using Unity.Builder;
|
||||
using Unity.Exceptions;
|
||||
|
@ -16,12 +14,6 @@ namespace Unity
|
|||
{
|
||||
#region PipelineBuilder
|
||||
|
||||
public override IEnumerable<Expression> Build(UnityContainer container, IEnumerator<Pipeline> enumerator,
|
||||
Type type, IRegistration registration)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
public override ResolveDelegate<BuilderContext>? Build(ref PipelineBuilder builder)
|
||||
{
|
||||
// Skip if already have a resolver
|
||||
|
@ -29,7 +21,7 @@ namespace Unity
|
|||
|
||||
// Try to get resolver
|
||||
Type? generic = null;
|
||||
var resolver = builder.Registration?.Get(typeof(ResolveDelegate<BuilderContext>)) ??
|
||||
var resolver = builder.Policies?.Get(typeof(ResolveDelegate<BuilderContext>)) ??
|
||||
builder.ContainerContext.Get(builder.Type, typeof(ResolveDelegate<BuilderContext>));
|
||||
|
||||
if (null == resolver)
|
||||
|
@ -50,9 +42,9 @@ namespace Unity
|
|||
if (null != resolver) return builder.Pipeline((ResolveDelegate<BuilderContext>)resolver);
|
||||
|
||||
// Try finding factory
|
||||
TypeFactoryDelegate? factory = builder.Registration?.Get<TypeFactoryDelegate>();
|
||||
TypeFactoryDelegate? factory = builder.Policies?.Get<TypeFactoryDelegate>();
|
||||
|
||||
if (builder.Registration is ExplicitRegistration @explicit)
|
||||
if (builder.Policies is ExplicitRegistration @explicit)
|
||||
{
|
||||
#if NETCOREAPP1_0 || NETSTANDARD1_0
|
||||
if (null != builder.Type && builder.Type.GetTypeInfo().IsGenericType)
|
||||
|
@ -67,7 +59,7 @@ namespace Unity
|
|||
{
|
||||
if (builder.Type.GetArrayRank() == 1)
|
||||
{
|
||||
var resolve = ArrayResolver.Factory(builder.Type, (IRegistration)builder.Registration);
|
||||
var resolve = ArrayResolver.Factory(builder.Type, builder.ContainerContext.Container);
|
||||
return builder.Pipeline((ref BuilderContext context) => resolve(ref context));
|
||||
}
|
||||
else
|
||||
|
@ -77,7 +69,7 @@ namespace Unity
|
|||
}
|
||||
}
|
||||
}
|
||||
else if(builder.Registration is ImplicitRegistration @implicit)
|
||||
else if(builder.Policies is ImplicitRegistration @implicit)
|
||||
{
|
||||
#if NETCOREAPP1_0 || NETSTANDARD1_0
|
||||
if (null != builder.Type && builder.Type.GetTypeInfo().IsGenericType)
|
||||
|
@ -91,7 +83,7 @@ namespace Unity
|
|||
else if (builder.Type?.IsArray ?? false)
|
||||
{
|
||||
if (builder.Type?.GetArrayRank() == 1)
|
||||
return builder.Pipeline(ArrayResolver.Factory(builder.Type, @implicit));
|
||||
return builder.Pipeline(ArrayResolver.Factory(builder.Type, builder.ContainerContext.Container));
|
||||
else
|
||||
{
|
||||
var message = $"Invalid array {builder.Type}. Only arrays of rank 1 are supported";
|
||||
|
@ -103,7 +95,7 @@ namespace Unity
|
|||
Debug.Assert(null != builder.Type);
|
||||
|
||||
return null != factory
|
||||
? builder.Pipeline(factory(builder.Type, (IRegistration)builder.Registration))
|
||||
? builder.Pipeline(factory(builder.Type, builder.ContainerContext.Container))
|
||||
: builder.Pipeline();
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace Unity
|
|||
{
|
||||
var requestedType = builder.Type;
|
||||
|
||||
if (builder.Registration is ExplicitRegistration registration)
|
||||
if (builder.Policies is ExplicitRegistration registration)
|
||||
{
|
||||
// Explicit Registration
|
||||
if (null == registration.Type) return builder.Pipeline();
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace Unity
|
|||
{
|
||||
var requestedType = builder.Type;
|
||||
|
||||
if (builder.Registration is ExplicitRegistration @explicit)
|
||||
if (builder.Policies is ExplicitRegistration @explicit)
|
||||
{
|
||||
// Explicit Registration
|
||||
if (null == @explicit.Type) return builder.Pipeline();
|
||||
|
@ -21,7 +21,7 @@ namespace Unity
|
|||
? @explicit.Type
|
||||
: @explicit.BuildType(@explicit.Type);
|
||||
}
|
||||
else if (builder.Registration is ImplicitRegistration @implicit)
|
||||
else if (builder.Policies is ImplicitRegistration @implicit)
|
||||
{
|
||||
// Implicit Registration
|
||||
if (null != @implicit.BuildType)
|
||||
|
|
|
@ -3,23 +3,11 @@ using System.Collections.Generic;
|
|||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using Unity.Injection;
|
||||
using Unity.Registration;
|
||||
|
||||
namespace Unity
|
||||
{
|
||||
public abstract partial class MemberPipeline<TMemberInfo, TData> where TMemberInfo : MemberInfo
|
||||
{
|
||||
#region PipelineBuilder
|
||||
|
||||
public override IEnumerable<Expression> Build(UnityContainer container, IEnumerator<Pipeline> enumerator,
|
||||
Type type, IRegistration registration)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Selection Processing
|
||||
|
||||
protected virtual IEnumerable<Expression> ExpressionsFromSelection(Type type, IEnumerable<object> members)
|
||||
|
|
|
@ -111,27 +111,27 @@ namespace Unity
|
|||
|
||||
#region MemberProcessor
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Call hierarchy:
|
||||
/// <see cref="GetExpressions"/>
|
||||
/// + <see cref="SelectMembers"/>
|
||||
/// + <see cref="ExpressionsFromSelected"/>
|
||||
/// + <see cref="BuildMemberExpression"/>
|
||||
/// + <see cref="GetResolverExpression"/>
|
||||
/// </remarks>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="registration"></param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<Expression> GetExpressions(Type type, IRegistration? registration)
|
||||
{
|
||||
var selector = GetOrDefault(registration);
|
||||
var members = selector(type, registration?.InjectionMembers);
|
||||
///// <summary>
|
||||
/////
|
||||
///// </summary>
|
||||
///// <remarks>
|
||||
///// Call hierarchy:
|
||||
///// <see cref="GetExpressions"/>
|
||||
///// + <see cref="SelectMembers"/>
|
||||
///// + <see cref="ExpressionsFromSelected"/>
|
||||
///// + <see cref="BuildMemberExpression"/>
|
||||
///// + <see cref="GetResolverExpression"/>
|
||||
///// </remarks>
|
||||
///// <param name="type"></param>
|
||||
///// <param name="registration"></param>
|
||||
///// <returns></returns>
|
||||
//public IEnumerable<Expression> GetExpressions(Type type, IRegistration? registration)
|
||||
//{
|
||||
// var selector = GetOrDefault(registration);
|
||||
// var members = selector(type, registration?.InjectionMembers);
|
||||
|
||||
return ExpressionsFromSelection(type, members);
|
||||
}
|
||||
// return ExpressionsFromSelection(type, members);
|
||||
//}
|
||||
|
||||
#endregion
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ namespace Unity
|
|||
if (null != builder.Seed) return builder.Pipeline();
|
||||
|
||||
var pipeline = builder.Pipeline();
|
||||
var selector = GetOrDefault(builder.Registration);
|
||||
var selector = GetOrDefault(builder.Policies);
|
||||
var members = selector.Invoke(builder.Type, builder.InjectionMembers);
|
||||
var resolvers = ResolversFromSelection(builder.Type, members).ToArray();
|
||||
|
||||
|
|
|
@ -260,7 +260,7 @@ namespace Unity
|
|||
|
||||
}
|
||||
|
||||
private ResolveDelegate<BuilderContext> PipelineFromRegistration(ref HashKey key, IRegistration registration, int position)
|
||||
private ResolveDelegate<BuilderContext> PipelineFromRegistration(ref HashKey key, ExplicitRegistration registration, int position)
|
||||
{
|
||||
Debug.Assert(null != _registry);
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче