/// <summary>
        ///
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static bool ShouldCreate(this ServiceDescriptorConfiguration item)
        {
            bool returnValue = false;

            if (item.Condition != null)
            {
                returnValue = Configuration[item.Condition.Key] == item.Condition.Value;
            }
            else
            {
                returnValue = true;
            }

            return(returnValue);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="item"></param>
        /// <param name="typeSource"></param>
        /// <returns></returns>
        public static string ExtractTypeDefinition(this ServiceDescriptorConfiguration item, TypeSource typeSource)
        {
            string returnValue = null;

            if (typeSource == TypeSource.Implemenation)
            {
                returnValue = item.ImplementationType;
            }
            else
            {
                returnValue = item.ServiceType;
            }

            return(returnValue);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static ServiceDescriptor CreateServiceDescriptor(this ServiceDescriptorConfiguration item)
        {
            ServiceDescriptor returnValue = null;

            if (item.ShouldCreate())
            {
                //
                // Get the service type.
                //
                Type serviceType = item.FindType(TypeSource.Service);

                //
                // Get the implementation type.
                //
                Type implementationType = item.FindType(TypeSource.Implemenation);

                //
                // Check if the implementation type has any dependency properties.
                //
                IEnumerable <DependencyInfo> dependencyProperties = DependencyAttribute.GetDependencyProperties(implementationType);

                //
                // Select the lifetime.
                //
                ServiceLifetime lifetime = item.Lifetime == "Scoped" ? ServiceLifetime.Scoped : item.Lifetime == "Singleton" ? ServiceLifetime.Singleton : ServiceLifetime.Transient;

                //
                // Create the descriptor.
                //
                if (item.Properties == null && !dependencyProperties.Any())
                {
                    //
                    // Standard definition.
                    //
                    returnValue = ServiceDescriptor.Describe(serviceType, implementationType, lifetime);
                }
                else
                {
                    //
                    // Factory based definition.
                    //
                    returnValue = ServiceDescriptor.Describe(serviceType, sp => (new DependencyFactory(implementationType, item, dependencyProperties)).GetInstance(sp), lifetime);
                }
            }

            return(returnValue);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="item"></param>
        /// <param name="typeSource"></param>
        /// <returns></returns>
        public static Type FindType(this ServiceDescriptorConfiguration item, TypeSource typeSource)
        {
            Type returnValue = null;

            //
            // Get the actual type definition. The type definition
            // passed here can contain one or more aliases.
            //
            string actualTypeDefinition = item.TransformAlias(typeSource);

            //
            // Get the type.
            //
            returnValue = Type.GetType(actualTypeDefinition, true);

            return(returnValue);
        }
Esempio n. 5
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="implementationType"></param>
 /// <param name="configuration"></param>
 /// <param name="dependencyProperties"></param>
 public DependencyFactory(Type implementationType, ServiceDescriptorConfiguration configuration, IEnumerable <DependencyInfo> dependencyProperties)
 {
     this.ImplementationType   = implementationType;
     this.Configuration        = configuration;
     this.DependencyProperties = dependencyProperties;
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="item"></param>
 /// <param name="typeSource"></param>
 /// <returns></returns>
 public static string TransformAlias(this ServiceDescriptorConfiguration item, TypeSource typeSource)
 {
     return(TransformAlias(item.ExtractTypeDefinition(typeSource)));
 }