public void Registration(IEnumerable <RegistrationDefinition> definitions)
        {
            foreach (var definition in definitions)
            {
                ILifetime lifetime = null;

                switch (definition.RegistrationLifestyle)
                {
                case RegistrationLifestyle.Singleton:
                    lifetime = new PerContainerLifetime();
                    break;

                case RegistrationLifestyle.SingletonPerScope:
                    lifetime = new PerScopeLifetime();
                    break;
                }

                if (definition.RegistrationMode == RegistrationMode.Single)
                {
                    _container.Register(definition.ExportType, definition.ActivationType, lifetime);
                }
                else
                {
                    _container.Register(definition.ExportType, definition.ActivationType, definition.ActivationType.Name, lifetime);
                }
            }
        }
        private static ILifetime ResolveLifetime(ServiceDescriptor serviceDescriptor)
        {
            if (serviceDescriptor.ImplementationInstance != null)
            {
                return(null);
            }

            ILifetime lifetime = null;

            switch (serviceDescriptor.Lifetime)
            {
            case ServiceLifetime.Scoped:
                lifetime = new PerScopeLifetime();
                break;

            case ServiceLifetime.Singleton:
                lifetime = new PerContainerLifetime();
                break;

            case ServiceLifetime.Transient:
                lifetime = new PerRequestLifeTime();
                break;
            }

            return(lifetime);
        }
コード例 #3
0
        public void ShouldClonePerScopeLifetime()
        {
            var lifetime = new PerScopeLifetime();

            var clone = lifetime.Clone();

            Assert.IsType <PerScopeLifetime>(clone);
        }
コード例 #4
0
        /// <summary>
        /// Returns a service instance according to the specific lifetime characteristics.
        /// </summary>
        /// <param name="createInstance">The function delegate used to create a new service instance.</param>
        /// <param name="scope">The <see cref="T:LightInject.Scope" /> of the current service request.</param>
        /// <returns>
        /// The requested services instance.
        /// </returns>
        public object GetInstance(Func <object> createInstance, Scope scope)
        {
            bool hasContext = _httpContextAccessor?.HttpContext != null;

            if (hasContext)
            {
                PerScopeLifetime perScopeLifetime = new PerScopeLifetime();
                return(perScopeLifetime.GetInstance(createInstance, scope));
            }

            return(createInstance());
        }
コード例 #5
0
        private static ILifetime ResolveLifetime(ServiceDescriptor serviceDescriptor, Scope rootScope)
        {
            ILifetime lifetime = null;

            switch (serviceDescriptor.Lifetime)
            {
            case ServiceLifetime.Scoped:
                lifetime = new PerScopeLifetime();
                break;

            case ServiceLifetime.Singleton:
                lifetime = new PerRootScopeLifetime(rootScope);
                break;

            case ServiceLifetime.Transient:
                lifetime = NeedsTracking(serviceDescriptor) ? new PerRequestLifeTime() : null;
                break;
            }

            return(lifetime);
        }