Adding Pipeline to lifetime managers

This commit is contained in:
Eugene Sadovoi 2019-05-23 16:28:47 -04:00
Родитель c1d670f049
Коммит a264a8b9cb
6 изменённых файлов: 21 добавлений и 51 удалений

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

@ -12,8 +12,6 @@ namespace Unity.Registration
{
string? Name { get; }
PipelineDelegate? PipelineDelegate { get; set; }
ResolveDelegate<BuilderContext>? Pipeline { get; set; }
IEnumerable<Pipeline>? Processors { get; set; }

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

@ -20,6 +20,7 @@ namespace Unity.Registration
#region Fields
private int _refCount;
private ResolveDelegate<BuilderContext>? _pipeline;
#endregion
@ -65,9 +66,20 @@ namespace Unity.Registration
public string? Name { get; }
public virtual PipelineDelegate? PipelineDelegate { get; set; }
public ResolveDelegate<BuilderContext>? Pipeline { get; set; }
public ResolveDelegate<BuilderContext>? Pipeline
{
get => _pipeline; set
{
if (null != value && null != LifetimeManager &&
!(LifetimeManager is TransientLifetimeManager))
{
LifetimeManager.PipelineDelegate = value;
_pipeline = LifetimeManager.Pipeline;
}
else
_pipeline = value;
}
}
public IEnumerable<Pipeline>? Processors { get; set; }
@ -99,7 +111,7 @@ namespace Unity.Registration
{
return policyInterface switch
{
Type type when typeof(LifetimeManager) == type => base.Get(policyInterface) ?? LifetimeManager,
Type type when typeof(LifetimeManager) == type => base.Get(policyInterface) ?? LifetimeManager,
_ => base.Get(policyInterface)
};
}

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

@ -1,7 +1,6 @@
using System;
using System.Diagnostics;
using System.Security;
using System.Threading.Tasks;
using Unity.Builder;
using Unity.Lifetime;
using Unity.Resolution;
@ -24,7 +23,6 @@ namespace Unity.Registration
// Set Members
LifetimeManager = manager;
PipelineDelegate = OnResolve;
Pipeline = manager switch
{
ExternallyControlledLifetimeManager _ => ExternalLifetime,
@ -34,13 +32,6 @@ namespace Unity.Registration
#region Implementation
[SecuritySafeCritical]
private ValueTask<object?> OnResolve(ref BuilderContext context)
{
Debug.Assert(null != Pipeline);
return new ValueTask<object?>(Pipeline(ref context));
}
private object ExternalLifetime(ref BuilderContext context)
{
Debug.Assert(null != LifetimeManager);

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

@ -202,8 +202,7 @@ namespace Unity
}
return new ValueTask<object?>(Task.Factory
.StartNew<object?>(delegate
return new ValueTask<object?>(Task.Factory.StartNew<object?>(delegate
{
// Check if already created and acquire a lock
if (null != registration.LifetimeManager)
@ -264,36 +263,4 @@ namespace Unity
#endregion
}
// Backups
/*
ValueTask<object?> IUnityContainerAsync.ResolveAsync(Type type, string? name, params ResolverOverride[] overrides)
{
// Setup Context
var pipeline = GetPipeline(type ?? throw new ArgumentNullException(nameof(type)), name);
// Execute pipeline
var context = new PipelineContext
{
Type = type,
Name = name,
RunAsync = true,
Overrides = overrides,
ContainerContext = Context,
};
try
{
return pipeline(ref context);
}
catch (Exception ex)
when (ex is InvalidRegistrationException ||
ex is CircularDependencyException ||
ex is ObjectDisposedException)
{
var message = CreateMessage(ex);
throw new ResolutionFailedException(context.Type, context.Name, message, ex);
}
}
*/
}

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

@ -298,6 +298,9 @@ namespace Unity
Debug.Assert(null != container);
return container?.Container;
}
internal override object Pipeline<TContext>(ref TContext context)
=> context.Container;
}
}
}

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

@ -62,8 +62,7 @@ namespace Unity
// IUnityContainer, IUnityContainerAsync
var container = new ExplicitRegistration(this, null, typeof(UnityContainer), new ContainerLifetimeManager())
{
Pipeline = (ref BuilderContext c) => c.Container,
PipelineDelegate = (ref BuilderContext c) => new ValueTask<object?>(c.Container)
Pipeline = (ref BuilderContext c) => c.Container
};
// Create Registries