RegistrationBuilder
class RegistrationBuilder
using Stashbox.Lifetime;
using Stashbox.Registration.Fluent;
using Stashbox.Utils;
using System;
namespace Stashbox.Registration
{
internal class RegistrationBuilder
{
public ServiceRegistration BuildServiceRegistration(IContainerContext containerContext, RegistrationConfiguration registrationConfiguration, bool isDecorator)
{
PreProcessExistingInstanceIfNeeded(containerContext, registrationConfiguration.Context, registrationConfiguration.ImplementationType);
if (!isDecorator)
registrationConfiguration.Context.Lifetime = ChooseLifeTime(containerContext, registrationConfiguration.Context);
return new ServiceRegistration(registrationConfiguration.ImplementationType, DetermineRegistrationType(registrationConfiguration), containerContext.ContainerConfiguration, registrationConfiguration.Context, isDecorator);
}
private void PreProcessExistingInstanceIfNeeded(IContainerContext containerContext, RegistrationContext registrationContext, Type implementationType)
{
if (registrationContext.ExistingInstance != null) {
if (!registrationContext.IsLifetimeExternallyOwned && implementationType.IsDisposable())
containerContext.RootScope.AddDisposableTracking(registrationContext.ExistingInstance);
if ((object)registrationContext.Finalizer != null)
Constants.AddWithFinalizerMethod.MakeGenericMethod(implementationType).Invoke(containerContext.RootScope, new object[2] {
registrationContext.ExistingInstance,
registrationContext.Finalizer
});
}
}
private LifetimeDescriptor ChooseLifeTime(IContainerContext containerContext, RegistrationContext registrationContext)
{
object lifetimeDescriptor;
if (!registrationContext.IsWireUp) {
lifetimeDescriptor = registrationContext.Lifetime;
if (lifetimeDescriptor == null)
return containerContext.ContainerConfiguration.DefaultLifetime;
} else
lifetimeDescriptor = Lifetimes.Singleton;
return (LifetimeDescriptor)lifetimeDescriptor;
}
private RegistrationType DetermineRegistrationType(RegistrationConfiguration registrationConfiguration)
{
if (registrationConfiguration.ImplementationType.IsOpenGenericType())
return RegistrationType.OpenGeneric;
if (registrationConfiguration.Context.ExistingInstance != null) {
if (!registrationConfiguration.Context.IsWireUp)
return RegistrationType.Instance;
return RegistrationType.WireUp;
}
if ((object)registrationConfiguration.Context.ContainerFactory == null && (object)registrationConfiguration.Context.SingleFactory == null)
return RegistrationType.Default;
return RegistrationType.Factory;
}
}
}