示例#1
0
        /// <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);
        }
示例#2
0
        /// <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));
        }
示例#3
0
        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);
        }
示例#4
0
        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));
        }
示例#5
0
        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);
        }
示例#6
0
        /// <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);
        }
示例#7
0
        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 });
        }
示例#8
0
 private ILifetime ChooseLifeTime(IRegistrationContextMeta meta) => meta.Context.ExistingInstance != null
     ? meta.Context.IsWireUp
         ? new SingletonLifetime()
         : null
     : meta.Context.Lifetime;
示例#9
0
 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);
示例#10
0
 private ILifetime ChooseLifeTime(IRegistrationContextMeta meta) => meta.Context.ExistingInstance != null
     ? null : meta.Context.Lifetime;