Пример #1
0
        private static Type GetBaseType(DependencyLifeTimeScope lifeTimeScope)
        {
            switch (lifeTimeScope)
            {
            case DependencyLifeTimeScope.Indepentdent:
                return(typeof(IIndependentDependency));

            case DependencyLifeTimeScope.Request:
                return(typeof(IRequestDenpendency));

            case DependencyLifeTimeScope.Singleton:
                return(typeof(ISingletonDependency));

            default:
                return(null);
            }
        }
Пример #2
0
        public static void RegisterDependency(this ContainerBuilder builder, Assembly[] assemblies, DependencyLifeTimeScope lifeTimeScope)
        {
            Type baseType = GetBaseType(lifeTimeScope);

            switch (lifeTimeScope)
            {
            case DependencyLifeTimeScope.Singleton:
                builder.RegisterAssemblyTypes(assemblies)
                .Where(type => baseType.IsAssignableFrom(type) && !type.IsAbstract)
                .AsSelf().AsImplementedInterfaces()
                .PropertiesAutowired().SingleInstance();
                break;

            case DependencyLifeTimeScope.Indepentdent:
                builder.RegisterAssemblyTypes(assemblies)
                .Where(type => baseType.IsAssignableFrom(type) && !type.IsAbstract)
                .AsSelf().AsImplementedInterfaces()
                .PropertiesAutowired().InstancePerDependency();
                break;

            case DependencyLifeTimeScope.Request:
                builder.RegisterAssemblyTypes(assemblies)
                .Where(type => baseType.IsAssignableFrom(type) && !type.IsAbstract)
                .AsSelf().AsImplementedInterfaces()
                .PropertiesAutowired().InstancePerLifetimeScope();
                break;
            }
        }
Пример #3
0
        public static void RegisterGenericDependency(this ContainerBuilder builder, Assembly[] assemblies, DependencyLifeTimeScope lifeTimeScope)
        {
            List <Type> types = null;

            foreach (var assembly in assemblies)
            {
                var assemblyTypes = assembly.GetTypes().ToList();
                if (types == null)
                {
                    types = assemblyTypes;
                }
                types = types.Concat(assemblyTypes).ToList();
            }

            if (types == null)
            {
                return;
            }

            Type baseType = GetBaseType(lifeTimeScope);

            types = types.Where(baseType.IsAssignableFrom).ToList();

            var genericTypeIntefaces = types.Where(type => type.IsGenericType && type.IsInterface).ToList();
            var genericTypeInstances = types.Where(type => type.IsGenericType && !type.IsAbstract).ToList();

            Dictionary <Type, Type> genericTypeDic = new Dictionary <Type, Type>();

            foreach (Type gInterface in genericTypeIntefaces)
            {
                foreach (Type gInstance in genericTypeInstances)
                {
                    if (gInterface.Name.Contains(gInstance.Name))
                    {
                        genericTypeDic.Add(gInterface, gInstance);
                    }
                }
            }

            foreach (var generic in genericTypeDic)
            {
                switch (lifeTimeScope)
                {
                case DependencyLifeTimeScope.Singleton:
                    builder.RegisterGeneric(generic.Value).As(generic.Key).SingleInstance();
                    break;

                case DependencyLifeTimeScope.Indepentdent:
                    builder.RegisterGeneric(generic.Value).As(generic.Key).InstancePerDependency();
                    break;

                case DependencyLifeTimeScope.Request:
                    builder.RegisterGeneric(generic.Value).As(generic.Key).InstancePerLifetimeScope();
                    break;
                }
            }
        }