This commit is contained in:
Eugene Sadovoi 2019-05-21 13:47:10 -04:00
Родитель 4f0b5f471e
Коммит 3eba33f6ce
19 изменённых файлов: 130 добавлений и 100 удалений

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

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

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

@ -2,6 +2,7 @@
using System.Linq;
using System.Reflection;
using Unity.Builder;
using Unity.Registration;
using Unity.Resolution;
using Unity.Storage;
@ -23,7 +24,7 @@ namespace Unity.Factories
#region TypeResolverFactory
public static TypeFactoryDelegate Factory = (Type type, PolicySet policies) =>
public static TypeFactoryDelegate Factory = (Type type, IRegistration policies) =>
{
var typeArgument = type.GetElementType();
var targetType = policies.Owner.GetTargetType(typeArgument);

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

@ -2,8 +2,8 @@
using System.Linq;
using System.Reflection;
using Unity.Builder;
using Unity.Registration;
using Unity.Resolution;
using Unity.Storage;
namespace Unity.Factories
{
@ -24,7 +24,7 @@ namespace Unity.Factories
#region TypeResolverFactory
public static TypeFactoryDelegate Factory = (Type type, PolicySet policies) =>
public static TypeFactoryDelegate Factory = (Type type, IRegistration policies) =>
{
#if NETSTANDARD1_0 || NETCOREAPP1_0 || NET40
var typeArgument = type.GetTypeInfo().GenericTypeArguments.First();

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

@ -1,8 +1,8 @@
using System;
using System.Reflection;
using Unity.Builder;
using Unity.Registration;
using Unity.Resolution;
using Unity.Storage;
namespace Unity.Factories
{
@ -18,7 +18,7 @@ namespace Unity.Factories
#region TypeResolverFactory
public static TypeFactoryDelegate Factory = (Type type, PolicySet policies) =>
public static TypeFactoryDelegate Factory = (Type type, IRegistration policies) =>
{
var typeToBuild = type.GetTypeInfo().GenericTypeArguments[0];
var factoryMethod = ImplementationMethod.MakeGenericMethod(typeToBuild);

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

@ -2,6 +2,7 @@
using System.Reflection;
using Unity.Builder;
using Unity.Lifetime;
using Unity.Registration;
using Unity.Resolution;
using Unity.Storage;
@ -24,7 +25,7 @@ namespace Unity.Factories
#region TypeResolverFactory
public static TypeFactoryDelegate Factory = (Type type, PolicySet policies) =>
public static TypeFactoryDelegate Factory = (Type type, IRegistration policies) =>
{
var itemType = type.GetTypeInfo().GenericTypeArguments[0];
var lazyMethod = ImplementationMethod.MakeGenericMethod(itemType);

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

@ -2,8 +2,8 @@
using System.Linq;
using System.Reflection;
using Unity.Builder;
using Unity.Registration;
using Unity.Resolution;
using Unity.Storage;
namespace Unity.Factories
{
@ -28,7 +28,7 @@ namespace Unity.Factories
#region ResolveDelegateFactory
public static TypeFactoryDelegate Factory = (Type type, PolicySet policies) =>
public static TypeFactoryDelegate Factory = (Type type, IRegistration policies) =>
{
#if NETSTANDARD1_0 || NETCOREAPP1_0 || NET40

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

@ -1,9 +1,9 @@
using System;
using Unity.Builder;
using Unity.Registration;
using Unity.Resolution;
using Unity.Storage;
namespace Unity
{
public delegate ResolveDelegate<BuilderContext> TypeFactoryDelegate(Type type, PolicySet policies);
public delegate ResolveDelegate<BuilderContext> TypeFactoryDelegate(Type type, IRegistration policies);
}

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

@ -61,7 +61,7 @@ namespace Unity
public readonly ContainerContext ContainerContext;
public readonly ImplicitRegistration Registration;
public readonly IRegistration Registration;
#endregion

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

@ -5,6 +5,7 @@ using Unity.Builder;
using Unity.Exceptions;
using Unity.Injection;
using Unity.Lifetime;
using Unity.Policy;
using Unity.Resolution;
namespace Unity
@ -38,8 +39,8 @@ namespace Unity
}
// Select ConstructorInfo
var selector = GetOrDefault(builder.Registration);
var selection = selector.Invoke(builder.Type, builder.Registration)
var selector = GetOrDefault((IPolicySet)builder.Registration);
var selection = selector.Invoke(builder.Type, (IPolicySet)builder.Registration)
.FirstOrDefault();
// Select constructor for the Type

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

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;
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; }
PipelineDelegate? PipelineDelegate { get; set; }
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; }
}
}

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

@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using Unity.Builder;
using Unity.Lifetime;
using Unity.Resolution;

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

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

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

@ -54,7 +54,7 @@ namespace Unity.Storage
// Set current box to next item in collection.
if (null == _current)
_current = new ContainerRegistration(_entries[_index].Type, _entries[_index].Registration);
_current = new RegistrationWrapper(_entries[_index].Type, _entries[_index].Registration);
return true;
}

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

@ -2,15 +2,16 @@
using System.Diagnostics;
using Unity.Lifetime;
using Unity.Policy;
using Unity.Registration;
namespace Unity.Registration
namespace Unity.Storage
{
[DebuggerDisplay("RegisteredType={RegisteredType?.Name}, Name={Name}, MappedTo={RegisteredType == MappedToType ? string.Empty : MappedToType?.Name ?? string.Empty}, {LifetimeManager?.GetType()?.Name}")]
internal sealed class ContainerRegistration : IContainerRegistration
internal sealed class RegistrationWrapper : IContainerRegistration
{
private readonly ExplicitRegistration _registration;
public ContainerRegistration(Type type, IPolicySet registration)
public RegistrationWrapper(Type type, IPolicySet registration)
{
RegisteredType = type;
_registration = (ExplicitRegistration)registration;

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

@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Security;
using Unity.Policy;
using Unity.Registration;
namespace Unity.Storage
{
@ -103,6 +104,53 @@ namespace Unity.Storage
#region Public Members
internal void Set(Type type, IPolicySet set)
{
var key = new HashKey(type);
var targetBucket = key.HashCode % Buckets.Length;
for (var i = Buckets[targetBucket]; i >= 0; i = Entries[i].Next)
{
ref var candidate = ref Entries[i];
if (candidate.Key != key) continue;
candidate.Policies = set;
return;
}
ref var entry = ref Entries[Count];
entry.Key = key;
entry.Next = Buckets[targetBucket];
entry.Type = type;
entry.Policies = set;
Buckets[targetBucket] = Count++;
}
internal void Set(Type type, string? name, ExplicitRegistration registration)
{
var key = new HashKey(type, name);
var targetBucket = key.HashCode % Buckets.Length;
for (var i = Buckets[targetBucket]; i >= 0; i = Entries[i].Next)
{
ref var candidate = ref Entries[i];
if (candidate.Key != key) continue;
candidate.Policies = registration;
return;
}
ref var entry = ref Entries[Count];
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++;
}
public bool RequireToGrow => (Entries.Length - Count) < 100 &&
(float)Count / Entries.Length > 0.72f;
#endregion
@ -118,7 +166,8 @@ namespace Unity.Storage
public Type Type;
public bool IsExplicit;
public IPolicySet Policies;
public IContainerRegistration Cashe;
public IContainerRegistration Cache;
public IRegistration Registration;
}
#endregion

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

@ -10,7 +10,6 @@ using Unity.Events;
using Unity.Extension;
using Unity.Factories;
using Unity.Lifetime;
using Unity.Policy;
using Unity.Registration;
using Unity.Storage;
using Unity.Utility;
@ -203,8 +202,8 @@ namespace Unity
set.Add(ref _root._registry.Entries[2].Key);
// IUnityContainer & IUnityContainerAsync
yield return _root._registry.Entries[1].Cashe;
yield return _root._registry.Entries[2].Cashe;
yield return _root._registry.Entries[1].Cache;
yield return _root._registry.Entries[2].Cache;
// Explicit registrations
for (UnityContainer? container = this; null != container; container = container._parent)
@ -220,13 +219,13 @@ namespace Unity
if (!registry.Entries[i].IsExplicit || !set.Add(ref registry.Entries[i].Key))
continue;
cashe = registry.Entries[i].Cashe;
cashe = registry.Entries[i].Cache;
// Create wrapper is required
if (null == cashe)
{
cashe = new ContainerRegistration(registry.Entries[i].Type, registry.Entries[i].Policies);
registry.Entries[i].Cashe = cashe;
cashe = new RegistrationWrapper(registry.Entries[i].Type, registry.Entries[i].Policies);
registry.Entries[i].Cache = cashe;
}
yield return cashe;

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

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using Unity.Lifetime;
@ -290,13 +291,12 @@ namespace Unity
return null;
}
private ImplicitRegistration GetOrAdd(Type type, string? name, IPolicySet? factory = null)
private IEnumerable<IPolicySet> AddOrReplace(IEnumerable<Type> type, string? name, ExplicitRegistration registration)
{
var key = new HashKey(type, name);
return GetOrAdd(ref key, type, name, factory);
throw new NotImplementedException();
}
private ImplicitRegistration GetOrAdd(ref HashKey key, Type type, string? name, IPolicySet? factory)
private ImplicitRegistration GetOrAdd(ref HashKey key, Type type, string? name, IPolicySet? factory = null)
{
Debug.Assert(null != _registry);

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

@ -189,7 +189,8 @@ namespace Unity
{
try
{
var item = container.GetOrAdd(typeof(TElement), registration.Name, registration);
var itemKey = new HashKey(typeof(TElement), registration.Name);
var item = container.GetOrAdd(ref itemKey, typeof(TElement), registration.Name, registration);
value = resolve(typeof(TElement), item);
}
catch (MakeGenericTypeFailedException) { continue; }
@ -301,7 +302,8 @@ namespace Unity
{
object? value;
var set = new QuickSet();
var key = new HashKey(type);
var key = new HashKey(typeof(TElement));
var keyType = new HashKey(type);
var keyGeneric = new HashKey(typeDefinition);
// Iterate over hierarchy
@ -315,7 +317,7 @@ namespace Unity
var registry = container._registry;
// Get indexes for bound types and iterate over them
var length = container._metadata.GetMeta(ref key, out int[]? data);
var length = container._metadata.GetMeta(ref keyType, out int[]? data);
if (null != data)
{
for (var i = 1; i < length; i++)
@ -352,7 +354,8 @@ namespace Unity
{
try
{
var item = container.GetOrAdd(typeof(TElement), registration.Name, registration);
var itemKey = new HashKey(typeof(TElement), registration.Name);
var item = container.GetOrAdd(ref itemKey, typeof(TElement), registration.Name, registration);
value = resolve(typeof(TElement), item);
}
catch (MakeGenericTypeFailedException) { continue; }
@ -369,7 +372,8 @@ namespace Unity
{
object? value;
var set = new QuickSet();
var key = new HashKey(type);
var key = new HashKey(typeof(TElement));
var typeKey = new HashKey(type);
// Iterate over hierarchy
for (UnityContainer? container = this; null != container; container = container._parent)
@ -382,7 +386,7 @@ namespace Unity
var registry = container._registry;
// Get indexes and iterate over them
var length = container._metadata.GetMeta(ref key, out int[]? data);
var length = container._metadata.GetMeta(ref typeKey, out int[]? data);
if (null != data)
{
for (var i = 1; i < length; i++)
@ -394,7 +398,8 @@ namespace Unity
{
try
{
var item = container.GetOrAdd(typeof(TElement), registration.Name, registration);
var itemKey = new HashKey(typeof(TElement), registration.Name);
var item = container.GetOrAdd(ref itemKey, typeof(TElement), registration.Name, registration);
value = resolve(typeof(TElement), item);
}
catch (ArgumentException ex) when (ex.InnerException is TypeLoadException)
@ -440,7 +445,8 @@ namespace Unity
{
try
{
var item = container.GetOrAdd(typeof(TElement), registration.Name);
var itemKey = new HashKey(typeof(TElement), registration.Name);
var item = container.GetOrAdd(ref itemKey, typeof(TElement), registration.Name);
value = resolve(typeof(TElement), item);
}
catch (ArgumentException ex) when (ex.InnerException is TypeLoadException)
@ -466,7 +472,8 @@ namespace Unity
{
try
{
var item = container.GetOrAdd(typeof(TElement), registration.Name);
var itemKey = new HashKey(typeof(TElement), registration.Name);
var item = container.GetOrAdd(ref itemKey, typeof(TElement), registration.Name);
value = (TElement)resolve(typeof(TElement), item);
}
catch (MakeGenericTypeFailedException) { continue; }

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

@ -1,60 +0,0 @@
using System;
using Unity.Policy;
using Unity.Registration;
using Unity.Storage;
namespace Unity.Utility
{
internal static class RegistryExtensions
{
#region Set
internal static void Set(this Registry registry, Type type, IPolicySet set)
{
var key = new HashKey(type);
var targetBucket = key.HashCode % registry.Buckets.Length;
for (var i = registry.Buckets[targetBucket]; i >= 0; i = registry.Entries[i].Next)
{
ref var candidate = ref registry.Entries[i];
if (candidate.Key != key) continue;
candidate.Policies = set;
return;
}
ref var entry = ref registry.Entries[registry.Count];
entry.Key = key;
entry.Next = registry.Buckets[targetBucket];
entry.Type = type;
entry.Policies = set;
registry.Buckets[targetBucket] = registry.Count++;
}
internal static void Set(this Registry registry, Type type, string? name, ExplicitRegistration registration)
{
var key = new HashKey(type, name);
var targetBucket = key.HashCode % registry.Buckets.Length;
for (var i = registry.Buckets[targetBucket]; i >= 0; i = registry.Entries[i].Next)
{
ref var candidate = ref registry.Entries[i];
if (candidate.Key != key) continue;
candidate.Policies = registration;
return;
}
ref var entry = ref registry.Entries[registry.Count];
entry.Key = key;
entry.Next = registry.Buckets[targetBucket];
entry.IsExplicit = true;
entry.Type = type;
entry.Policies = registration;
entry.Cashe = new ContainerRegistration(type, registration);
registry.Buckets[targetBucket] = registry.Count++;
}
#endregion
}
}