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; } string? Name { get; }
PipelineDelegate? PipelineDelegate { get; set; }
ResolveDelegate<BuilderContext>? Pipeline { get; set; } ResolveDelegate<BuilderContext>? Pipeline { get; set; }
IEnumerable<Pipeline>? Processors { get; set; } IEnumerable<Pipeline>? Processors { get; set; }

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

@ -20,6 +20,7 @@ namespace Unity.Registration
#region Fields #region Fields
private int _refCount; private int _refCount;
private ResolveDelegate<BuilderContext>? _pipeline;
#endregion #endregion
@ -65,9 +66,20 @@ namespace Unity.Registration
public string? Name { get; } public string? Name { get; }
public virtual PipelineDelegate? PipelineDelegate { get; set; } public ResolveDelegate<BuilderContext>? Pipeline
{
public ResolveDelegate<BuilderContext>? Pipeline { get; set; } 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; } public IEnumerable<Pipeline>? Processors { get; set; }

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

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

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

@ -202,8 +202,7 @@ namespace Unity
} }
return new ValueTask<object?>(Task.Factory return new ValueTask<object?>(Task.Factory.StartNew<object?>(delegate
.StartNew<object?>(delegate
{ {
// Check if already created and acquire a lock // Check if already created and acquire a lock
if (null != registration.LifetimeManager) if (null != registration.LifetimeManager)
@ -264,36 +263,4 @@ namespace Unity
#endregion #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); Debug.Assert(null != container);
return container?.Container; return container?.Container;
} }
internal override object Pipeline<TContext>(ref TContext context)
=> context.Container;
} }
} }
} }

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

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