This commit is contained in:
Eugene Sadovoi 2019-06-08 15:44:53 -04:00
Родитель e3a5c04fb5
Коммит 2c4c9ad4a3
16 изменённых файлов: 129 добавлений и 192 удалений

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

@ -140,7 +140,7 @@ namespace Unity.Builder
public object? Existing { get; set; }
public IRegistration? Registration { get; set; }
public ExplicitRegistration? Registration { get; set; }
public ContainerContext ContainerContext { get; set; }

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

@ -164,7 +164,7 @@ namespace Unity
: Seed;
}
public ResolveDelegate<BuilderContext>? Pipeline(ResolveDelegate<BuilderContext>? method = null)
public ResolveDelegate<BuilderContext>? PipelineWithSeed(ResolveDelegate<BuilderContext>? method = null)
{
Seed = method;

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

@ -38,7 +38,7 @@ namespace Unity
}
// Process if found
if (null != resolver) return builder.Pipeline((ResolveDelegate<BuilderContext>)resolver);
if (null != resolver) return builder.PipelineWithSeed((ResolveDelegate<BuilderContext>)resolver);
// Try finding factory
TypeFactoryDelegate? factory = builder.Policies?.Get<TypeFactoryDelegate>();
@ -57,7 +57,7 @@ namespace Unity
if (builder.Type.GetArrayRank() == 1)
{
var resolve = ArrayResolver.Factory(builder.Type, builder.ContainerContext.Container);
return builder.Pipeline((ref BuilderContext context) => resolve(ref context));
return builder.PipelineWithSeed((ref BuilderContext context) => resolve(ref context));
}
else
{
@ -69,7 +69,7 @@ namespace Unity
Debug.Assert(null != builder.Type);
return null != factory
? builder.Pipeline(factory(builder.Type, builder.ContainerContext.Container))
? builder.PipelineWithSeed(factory(builder.Type, builder.ContainerContext.Container))
: builder.Pipeline();
}

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

@ -34,7 +34,7 @@ namespace Unity
var type = builder.Type;
return builder.Pipeline((ref BuilderContext context) =>
return builder.PipelineWithSeed((ref BuilderContext context) =>
{
try
{

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

@ -31,7 +31,7 @@ namespace Unity
var type = builder.Type;
return builder.Pipeline((ref BuilderContext context) => context.Resolve(type));
return builder.PipelineWithSeed((ref BuilderContext context) => context.Resolve(type));
}
#endregion

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

@ -1,29 +0,0 @@
using System;
using System.Collections.Generic;
using Unity.Builder;
using Unity.Injection;
using Unity.Lifetime;
using Unity.Policy;
using Unity.Resolution;
namespace Unity.Registration
{
public interface IRegistration : IPolicySet
{
string? Name { get; }
ResolveDelegate<BuilderContext>? Pipeline { get; set; }
IEnumerable<Pipeline>? Processors { get; set; }
InjectionMember[]? InjectionMembers { get; set; }
bool BuildRequired { get; }
Converter<Type, Type>? BuildType { get; }
LifetimeManager LifetimeManager { get; }
UnityContainer Owner { get; }
}
}

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

@ -14,7 +14,7 @@ namespace Unity.Registration
{
//[DebuggerDisplay("Registration.Implicit({Count})")]
//[DebuggerTypeProxy(typeof(ImplicitRegistrationDebugProxy))]
public class ImplicitRegistration : PolicySet, IRegistration
public class ImplicitRegistration : PolicySet
{
#region Fields

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

@ -123,7 +123,6 @@ namespace Unity.Storage
ref var entry = ref Entries[Count];
entry.Key = key;
entry.Next = Buckets[targetBucket];
entry.Type = type;
entry.Policies = set;
Buckets[targetBucket] = Count++;
}
@ -146,7 +145,6 @@ namespace Unity.Storage
entry.Key = key;
entry.Next = Buckets[targetBucket];
entry.IsExplicit = true;
entry.Type = type;
entry.Policies = registration;
entry.Cache = new RegistrationWrapper(type, registration);
Buckets[targetBucket] = Count++;
@ -185,13 +183,13 @@ namespace Unity.Storage
{
public HashKey Key;
public int Next;
public Type Type;
public bool IsExplicit;
public IPolicySet Policies;
public IContainerRegistration Cache;
public IRegistration Registration;
public ExplicitRegistration Registration;
public ResolveDelegate<BuilderContext>? Pipeline;
public int ID;
public Type Type { get; set; }
public bool IsExplicit;
public IContainerRegistration Cache;
}
#endregion

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

@ -111,7 +111,6 @@ namespace Unity
// Add registration
ref var entry = ref Container._registry.Entries[Container._registry.Count];
entry.Key = key;
entry.Type = type;
entry.Next = Container._registry.Buckets[targetBucket];
entry.Policies = new PolicySet(Container, policyInterface, policy);
Container._registry.Buckets[targetBucket] = Container._registry.Count++;
@ -149,7 +148,6 @@ namespace Unity
// Add registration
ref var entry = ref Container._registry.Entries[Container._registry.Count];
entry.Key = key;
entry.Type = type;
entry.Next = Container._registry.Buckets[targetBucket];
entry.Policies = new ImplicitRegistration(Container, name);
entry.Policies.Set(policyInterface, policy);

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

@ -148,7 +148,7 @@ namespace Unity
continue;
// Found a factory
return container.PipelineFromOpenGeneric(ref key, (ExplicitRegistration)candidate.Policies);
return container.PipelineFromOpenGeneric(ref key, candidate.Registration);
}
// Default factory
@ -171,33 +171,5 @@ namespace Unity
}
#endregion
#region Pipeline Creation
private ResolveDelegate<BuilderContext> DefaultBuildPipeline(LifetimeManager manager, Func<ResolveDelegate<BuilderContext>?> build)
{
ResolveDelegate<BuilderContext>? pipeline = null;
return (ref BuilderContext context) =>
{
if (null != pipeline) return pipeline(ref context);
lock (manager)
{
if (null == pipeline)
{
pipeline = build();
manager.PipelineDelegate = pipeline;
Debug.Assert(null != pipeline);
}
}
return pipeline(ref context);
};
}
#endregion
}
}

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

@ -1,6 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading;
using Unity.Builder;
using Unity.Lifetime;
using Unity.Registration;
@ -54,7 +53,7 @@ namespace Unity
manager.PipelineDelegate = manager switch
{
TransientLifetimeManager transient => PipelineFromOpenGenericTransient(type, factory, transient, _registry.Count),
SynchronizedLifetimeManager synch => PipelineFromOpenGenericSynchronized(type, factory, synch),
SynchronizedLifetimeManager synchro => PipelineFromOpenGenericSynchronized(type, factory, synchro),
PerResolveLifetimeManager peresolve => PipelineFromOpenGenericPerResolve(type, factory, peresolve),
_ => PipelineFromOpenGenericDefault(type, factory, manager)
};
@ -62,7 +61,6 @@ namespace Unity
// Create new entry
ref var entry = ref _registry.Entries[_registry.Count];
entry.Key = key;
entry.Type = type;
entry.Pipeline = manager.Pipeline;
entry.Next = _registry.Buckets[targetBucket];
position = _registry.Count++;

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

@ -51,7 +51,6 @@ namespace Unity
// Create new entry
ref var entry = ref _registry.Entries[_registry.Count];
entry.Key = key;
entry.Type = type;
entry.Pipeline = BuildPipeline;
entry.Next = _registry.Buckets[targetBucket];
position = _registry.Count++;

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

@ -71,7 +71,6 @@ namespace Unity
ref var entry = ref _registry.Entries[_registry.Count];
entry.Key = key;
entry.Next = _registry.Buckets[targetBucket];
entry.Type = key.Type;
entry.IsExplicit = true;
entry.Pipeline = manager.Pipeline;
int position = _registry.Count++;

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

@ -230,7 +230,10 @@ namespace Unity
// Create wrapper is required
if (null == cashe)
{
cashe = new RegistrationWrapper(registry.Entries[i].Type, registry.Entries[i].Policies);
var type = registry.Entries[i].Key.Type;
Debug.Assert(null != type);
cashe = new RegistrationWrapper(type, registry.Entries[i].Policies);
registry.Entries[i].Cache = cashe;
}

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

@ -88,8 +88,8 @@ namespace Unity
registration.Add(candidate.Policies);
}
candidate.Policies = registration;
candidate.Pipeline = PipelineFromRegistration(ref key, registration, i);
candidate.Registration = registration;
candidate.Pipeline = PipelineFromRegistration(ref key, registration, i);
// Replaced registration
return existing;
@ -106,11 +106,10 @@ namespace Unity
ref var entry = ref _registry.Entries[_registry.Count];
entry.Key = key;
entry.Next = _registry.Buckets[targetBucket];
entry.Type = type;
entry.IsExplicit = true;
entry.Policies = registration;
entry.Pipeline = PipelineFromRegistration(ref key, registration, _registry.Count);
entry.Registration = registration;
entry.Pipeline = PipelineFromRegistration(ref key, registration, _registry.Count);
int position = _registry.Count++;
_registry.Buckets[targetBucket] = position;

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

@ -11,151 +11,151 @@ using Unity.Utility;
namespace Unity
{
public partial class UnityContainer
{
#region Fields
// public partial class UnityContainer
// {
// #region Fields
private static readonly MethodInfo EnumerableMethod =
typeof(UnityContainer).GetTypeInfo()
.GetDeclaredMethod(nameof(UnityContainer.EnumerableHandler));
// private static readonly MethodInfo EnumerableMethod =
// typeof(UnityContainer).GetTypeInfo()
// .GetDeclaredMethod(nameof(UnityContainer.EnumerableHandler));
private static readonly MethodInfo EnumerableFactory =
typeof(UnityContainer).GetTypeInfo()
.GetDeclaredMethod(nameof(UnityContainer.ResolverFactory));
// private static readonly MethodInfo EnumerableFactory =
// typeof(UnityContainer).GetTypeInfo()
// .GetDeclaredMethod(nameof(UnityContainer.ResolverFactory));
#endregion
// #endregion
#region TypeResolverFactory
// #region TypeResolverFactory
public static TypeFactoryDelegate EnumerableTypeFactory = (Type type, UnityContainer container) =>
{
#if NETSTANDARD1_0 || NETCOREAPP1_0 || NET40
var typeArgument = type.GetTypeInfo().GenericTypeArguments.First();
if (typeArgument.GetTypeInfo().IsGenericType)
#else
Debug.Assert(0 < type.GenericTypeArguments.Length);
var typeArgument = type.GenericTypeArguments.First();
if (typeArgument.IsGenericType)
#endif
{
return ((EnumerableFactoryDelegate)
EnumerableFactory.MakeGenericMethod(typeArgument)
.CreateDelegate(typeof(EnumerableFactoryDelegate)))();
}
else
{
return (ResolveDelegate<BuilderContext>)
EnumerableMethod.MakeGenericMethod(typeArgument)
.CreateDelegate(typeof(ResolveDelegate<BuilderContext>));
}
};
// public static TypeFactoryDelegate EnumerableTypeFactory = (Type type, UnityContainer container) =>
// {
//#if NETSTANDARD1_0 || NETCOREAPP1_0 || NET40
// var typeArgument = type.GetTypeInfo().GenericTypeArguments.First();
// if (typeArgument.GetTypeInfo().IsGenericType)
//#else
// Debug.Assert(0 < type.GenericTypeArguments.Length);
// var typeArgument = type.GenericTypeArguments.First();
// if (typeArgument.IsGenericType)
//#endif
// {
// return ((EnumerableFactoryDelegate)
// EnumerableFactory.MakeGenericMethod(typeArgument)
// .CreateDelegate(typeof(EnumerableFactoryDelegate)))();
// }
// else
// {
// return (ResolveDelegate<BuilderContext>)
// EnumerableMethod.MakeGenericMethod(typeArgument)
// .CreateDelegate(typeof(ResolveDelegate<BuilderContext>));
// }
// };
#endregion
// #endregion
#region Implementation
// #region Implementation
private static object EnumerableHandler<TElement>(ref BuilderContext context)
{
return ((UnityContainer)context.Container).ResolveEnumerable<TElement>(context.Resolve,
context.Name);
}
// private static object EnumerableHandler<TElement>(ref BuilderContext context)
// {
// return ((UnityContainer)context.Container).ResolveEnumerable<TElement>(context.Resolve,
// context.Name);
// }
private static ResolveDelegate<BuilderContext> ResolverFactory<TElement>()
{
Type type = typeof(TElement).GetGenericTypeDefinition();
return (ref BuilderContext c) => ((UnityContainer)c.Container).ResolveEnumerable<TElement>(c.Resolve, type, c.Name);
}
// private static ResolveDelegate<BuilderContext> ResolverFactory<TElement>()
// {
// Type type = typeof(TElement).GetGenericTypeDefinition();
// return (ref BuilderContext c) => ((UnityContainer)c.Container).ResolveEnumerable<TElement>(c.Resolve, type, c.Name);
// }
#endregion
// #endregion
#region Nested Types
// #region Nested Types
private delegate ResolveDelegate<BuilderContext> EnumerableFactoryDelegate();
// private delegate ResolveDelegate<BuilderContext> EnumerableFactoryDelegate();
#endregion
// #endregion
#region Enumerator
// #region Enumerator
private class EnumerableEnumerator<TType> : IEnumerator<TType>
{
#region Fields
// private class EnumerableEnumerator<TType> : IEnumerator<TType>
// {
// #region Fields
private int _prime = 5;
private int[] Buckets;
private Entry[] Entries;
UnityContainer _container;
private int Count;
// private int _prime = 5;
// private int[] Buckets;
// private Entry[] Entries;
// UnityContainer _container;
// private int Count;
#endregion
// #endregion
#region Constructors
// #region Constructors
public EnumerableEnumerator(UnityContainer container)
{
_container = container;
var size = HashHelpers.Primes[_prime];
Buckets = new int[size];
Entries = new Entry[size];
// public EnumerableEnumerator(UnityContainer container)
// {
// _container = container;
// var size = HashHelpers.Primes[_prime];
// Buckets = new int[size];
// Entries = new Entry[size];
#if !NET40
unsafe
{
fixed (int* bucketsPtr = Buckets)
{
int* ptr = bucketsPtr;
var end = bucketsPtr + Buckets.Length;
while (ptr < end) *ptr++ = -1;
}
}
#else
for(int i = 0; i < Buckets.Length; i++)
Buckets[i] = -1;
#endif
}
//#if !NET40
// unsafe
// {
// fixed (int* bucketsPtr = Buckets)
// {
// int* ptr = bucketsPtr;
// var end = bucketsPtr + Buckets.Length;
// while (ptr < end) *ptr++ = -1;
// }
// }
//#else
// for(int i = 0; i < Buckets.Length; i++)
// Buckets[i] = -1;
//#endif
// }
#endregion
// #endregion
#region IEnumerator
// #region IEnumerator
public TType Current => throw new NotImplementedException();
// public TType Current => throw new NotImplementedException();
object IEnumerator.Current => throw new NotImplementedException();
// object IEnumerator.Current => throw new NotImplementedException();
public void Dispose()
{
throw new NotImplementedException();
}
// public void Dispose()
// {
// throw new NotImplementedException();
// }
public bool MoveNext()
{
throw new NotImplementedException();
}
// public bool MoveNext()
// {
// throw new NotImplementedException();
// }
public void Reset()
{
throw new NotImplementedException();
}
// public void Reset()
// {
// throw new NotImplementedException();
// }
#endregion
}
// #endregion
// }
#endregion
// #endregion
#region Entry Type
// #region Entry Type
private struct Entry
{
public HashKey Key;
public int Next;
}
// private struct Entry
// {
// public HashKey Key;
// public int Next;
// }
#endregion
}
// #endregion
// }
}