/// <inheritdoc /> public IStashboxContainer Register(IRegistrationContextMeta registrationContextMeta, bool isDecorator, bool replace) { var registration = this.CreateServiceRegistration(registrationContextMeta, isDecorator); if (isDecorator) { this.containerContext.DecoratorRepository.AddDecorator(registrationContextMeta.ServiceType, registration, false, replace); this.containerContext.DelegateRepository.InvalidateDelegateCache(); } else { var name = registration.RegistrationContext.Name ?? registration.ImplementationType; this.containerContext.RegistrationRepository.AddOrUpdateRegistration(registration, name, false, replace); } if (replace) { this.containerContext.DelegateRepository.InvalidateDelegateCache(); } this.containerExtensionManager.ExecuteOnRegistrationExtensions(this.containerContext, registration); return(this.containerContext.Container); }
/// <inheritdoc /> public IServiceRegistration CreateServiceRegistration(IRegistrationContextMeta registrationContextMeta, bool isDecorator) { registrationContextMeta.Context.Lifetime = this.ChooseLifeTime(registrationContextMeta); var objectBuilder = this.CreateObjectBuilder(registrationContextMeta); var shouldHandleDisposal = this.ShouldHandleDisposal(registrationContextMeta, objectBuilder); return(this.ProduceServiceRegistration(objectBuilder, registrationContextMeta, isDecorator, shouldHandleDisposal)); }
private bool ShouldHandleDisposal(IRegistrationContextMeta meta, IObjectBuilder objectBuilder) { if (meta.Context.IsLifetimeExternallyOwned) { return(false); } if (meta.Context.Lifetime == null && this.containerContext.ContainerConfigurator.ContainerConfiguration.TrackTransientsForDisposalEnabled) { return(true); } return(meta.Context.Lifetime != null || objectBuilder.HandlesObjectLifecycle); }
private IObjectBuilder CreateObjectBuilder(IRegistrationContextMeta meta) { if (meta.ImplementationType.IsOpenGenericType()) { return(this.objectBuilderSelector.Get(ObjectBuilder.Generic)); } if (meta.Context.ExistingInstance != null) { return(this.objectBuilderSelector.Get(ObjectBuilder.Instance)); } return(meta.Context.ContainerFactory != null? this.objectBuilderSelector.Get(ObjectBuilder.Factory) : this.objectBuilderSelector.Get(meta.Context.SingleFactory != null ? ObjectBuilder.Factory : ObjectBuilder.Default)); }
private bool ShouldHandleDisposal(IRegistrationContextMeta meta) { if (meta.Context.IsLifetimeExternallyOwned) { return(false); } if (meta.Context.ExistingInstance != null) { return(false); } if (meta.Context.Lifetime == null && this.containerContext.ContainerConfigurator.ContainerConfiguration.TrackTransientsForDisposalEnabled) { return(true); } return(meta.Context.Lifetime != null); }
/// <inheritdoc /> public IStashboxContainer ReMap(IRegistrationContextMeta registrationContextMeta, bool isDecorator) { var registration = this.CreateServiceRegistration(registrationContextMeta, isDecorator); if (isDecorator) { this.containerContext.DecoratorRepository.AddDecorator(registrationContextMeta.ServiceType, registration, true, false); } else { this.containerContext.RegistrationRepository.AddOrUpdateRegistration(registration, true, false); } this.containerContext.Container.RootScope.InvalidateDelegateCache(); this.containerExtensionManager.ExecuteOnRegistrationExtensions(this.containerContext, registration); return(this.containerContext.Container); }
private void PreProcessExistingInstanceIfNeeded(IRegistrationContextMeta meta) { if (meta.Context.ExistingInstance == null) { return; } if (!meta.Context.IsLifetimeExternallyOwned && meta.Context.ExistingInstance is IDisposable disposable) { this.containerContext.Container.RootScope.AddDisposableTracking(disposable); } if (meta.Context.Finalizer == null) { return; } var method = Constants.AddWithFinalizerMethod.MakeGenericMethod(meta.ServiceType); method.Invoke(this.containerContext.Container.RootScope, new[] { meta.Context.ExistingInstance, meta.Context.Finalizer }); }
private ILifetime ChooseLifeTime(IRegistrationContextMeta meta) => meta.Context.ExistingInstance != null ? meta.Context.IsWireUp ? new SingletonLifetime() : null : meta.Context.Lifetime;
private IServiceRegistration ProduceServiceRegistration(IObjectBuilder objectBuilder, IRegistrationContextMeta meta, bool isDecorator, bool shouldHandleDisposal) => new ServiceRegistration(meta.ServiceType, meta.ImplementationType, this.containerContext.ContainerConfigurator, objectBuilder, meta.Context, isDecorator, shouldHandleDisposal);
private ILifetime ChooseLifeTime(IRegistrationContextMeta meta) => meta.Context.ExistingInstance != null ? null : meta.Context.Lifetime;