Stashbox by Peter Csajtai

<PackageReference Include="Stashbox" Version="3.1.0-preview-527" />

 RegistrationBuilder

using Stashbox.BuildUp; using Stashbox.Configuration; using Stashbox.Lifetime; using Stashbox.Registration.Fluent; using Stashbox.Utils; using System; namespace Stashbox.Registration { internal class RegistrationBuilder : IRegistrationBuilder { private readonly ContainerConfiguration containerConfiguration; private readonly IResolutionScope rootScope; private readonly IObjectBuilderSelector objectBuilderSelector; public RegistrationBuilder(ContainerConfiguration containerConfiguration, IResolutionScope rootScope, IObjectBuilderSelector objectBuilderSelector) { this.containerConfiguration = containerConfiguration; this.rootScope = rootScope; this.objectBuilderSelector = objectBuilderSelector; } public IServiceRegistration BuildServiceRegistration(RegistrationConfiguration registrationConfiguration, bool isDecorator) { PreProcessExistingInstanceIfNeeded(registrationConfiguration.Context, registrationConfiguration.ImplementationType); registrationConfiguration.Context.Lifetime = ChooseLifeTime(registrationConfiguration.Context); bool shouldHandleDisposal = ShouldHandleDisposal(registrationConfiguration.Context); return new ServiceRegistration(registrationConfiguration.ImplementationType, containerConfiguration, SelectObjectBuilder(registrationConfiguration.Context, registrationConfiguration.ImplementationType), registrationConfiguration.Context, isDecorator, shouldHandleDisposal); } private void PreProcessExistingInstanceIfNeeded(RegistrationContext registrationContext, Type implementationType) { if (registrationContext.ExistingInstance != null) { if (!registrationContext.IsLifetimeExternallyOwned) { IDisposable disposable = registrationContext.ExistingInstance as IDisposable; if (disposable != null) rootScope.AddDisposableTracking(disposable); } if (registrationContext.Finalizer != null) Constants.AddWithFinalizerMethod.MakeGenericMethod(implementationType).Invoke(rootScope, new object[2] { registrationContext.ExistingInstance, registrationContext.Finalizer }); } } private bool ShouldHandleDisposal(RegistrationContext registrationContext) { if (registrationContext.IsLifetimeExternallyOwned) return false; if (registrationContext.ExistingInstance != null) return false; if (registrationContext.Lifetime == null && containerConfiguration.TrackTransientsForDisposalEnabled) return true; return registrationContext.Lifetime != null; } private ILifetime ChooseLifeTime(RegistrationContext registrationContext) { object lifetime; if (registrationContext.ExistingInstance == null) { lifetime = registrationContext.Lifetime; if (lifetime == null) return containerConfiguration.DefaultLifetime?.Create(); } else { ILifetime lifetime2 = registrationContext.IsWireUp ? new SingletonLifetime() : null; lifetime = lifetime2; } return (ILifetime)lifetime; } private IObjectBuilder SelectObjectBuilder(RegistrationContext registrationContext, Type implementationType) { if (implementationType.IsOpenGenericType()) return objectBuilderSelector.Get(ObjectBuilder.Generic); if (registrationContext.ExistingInstance != null) { if (!registrationContext.IsWireUp) return objectBuilderSelector.Get(ObjectBuilder.Instance); return objectBuilderSelector.Get(ObjectBuilder.WireUp); } if (registrationContext.ContainerFactory == null) return objectBuilderSelector.Get((registrationContext.SingleFactory != null) ? ObjectBuilder.Factory : ObjectBuilder.Default); return objectBuilderSelector.Get(ObjectBuilder.Factory); } } }