FactoryLifetimeDescriptor
Represents a lifetime descriptor which applies to factory delegates.
using Stashbox.Registration;
using Stashbox.Resolution;
using System;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
namespace Stashbox.Lifetime
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public abstract class FactoryLifetimeDescriptor : LifetimeDescriptor
{
[return: System.Runtime.CompilerServices.Nullable(2)]
private protected override Expression BuildLifetimeAppliedExpression(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
{
Func<IResolutionScope, IRequestContext, object> factoryDelegateForRegistration = GetFactoryDelegateForRegistration(serviceRegistration, resolutionContext, typeInformation);
if (factoryDelegateForRegistration != null)
return ApplyLifetime(factoryDelegateForRegistration, serviceRegistration, resolutionContext, typeInformation.Type);
return null;
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1
})]
private static Func<IResolutionScope, IRequestContext, object> GetFactoryDelegateForRegistration(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
{
if (!LifetimeDescriptor.IsRegistrationOutputCacheable(serviceRegistration, resolutionContext))
return GetNewFactoryDelegate(serviceRegistration, resolutionContext.BeginSubGraph(), typeInformation);
Func<IResolutionScope, IRequestContext, object> orDefault = resolutionContext.FactoryCache.GetOrDefault(serviceRegistration.RegistrationId);
if (orDefault != null)
return orDefault;
orDefault = GetNewFactoryDelegate(serviceRegistration, resolutionContext.BeginSubGraph(), typeInformation);
if (orDefault == null)
return null;
resolutionContext.FactoryCache.Add(serviceRegistration.RegistrationId, orDefault);
return orDefault;
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1
})]
private static Func<IResolutionScope, IRequestContext, object> GetNewFactoryDelegate(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
{
Expression expressionForRegistration = LifetimeDescriptor.GetExpressionForRegistration(serviceRegistration, resolutionContext, typeInformation);
if (expressionForRegistration == null)
return null;
return expressionForRegistration.CompileDelegate(resolutionContext, resolutionContext.CurrentContainerContext.ContainerConfiguration);
}
protected abstract Expression ApplyLifetime(Func<IResolutionScope, IRequestContext, object> factory, ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, Type resolveType);
}
}