SingletonLifetime
Represents a singleton lifetime.
using Stashbox.Registration;
using Stashbox.Resolution;
using Stashbox.Utils;
using System;
using System.Linq.Expressions;
namespace Stashbox.Lifetime
{
public class SingletonLifetime : FactoryLifetimeDescriptor
{
protected override int LifeSpan => 20;
private protected override bool StoreResultInLocalVariable => true;
protected override Expression ApplyLifetime(Func<IResolutionScope, IRequestContext, object> factory, ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, Type resolveType)
{
return (resolutionContext.RequestInitiatorContainerContext.ContainerConfiguration.ReBuildSingletonsInChildContainerEnabled ? resolutionContext.RequestInitiatorContainerContext.RootScope : resolutionContext.CurrentContainerContext.RootScope).AsConstant().CallMethod(Constants.GetOrAddScopedObjectMethod, serviceRegistration.RegistrationId.AsConstant(), factory.AsConstant(), resolutionContext.RequestContextParameter, resolveType.AsConstant()).ConvertTo(resolveType);
}
}
}