Exemple #1
0
        public static void Registers(this IServiceCollection services, string startWithName, Assembly executingAssembly = null)
        {
            if (executingAssembly == null)
            {
                executingAssembly = Assembly.GetExecutingAssembly();
            }

            var assemblies = AssemblyFinder.Instance(startWithName).FindAll().Union(new[] { executingAssembly }).ToArray();

            if (!assemblies.Any())
            {
                return;
            }

            foreach (var assembly in assemblies)
            {
                var types = assembly.GetTypes();
                if (types.Length < 1)
                {
                    continue;
                }

                var lifetimeTypes = types.Where(type => typeof(ILifetimeDependency).IsAssignableFrom(type) && !type.IsAbstract);
                foreach (var item in lifetimeTypes.Where(t => !t.IsInterface))
                {
                    var interFaces = item.GetInterfaces();
                    foreach (var interFace in interFaces)
                    {
                        services.AddScoped(interFace, item);
                    }
                }

                var singleInstanceTypes = types.Where(type => typeof(ISingleInstanceDependency).IsAssignableFrom(type) && !type.IsAbstract);
                foreach (var item in singleInstanceTypes)
                {
                    var interFaces = item.GetInterfaces();
                    foreach (var interFace in interFaces)
                    {
                        services.AddSingleton(interFace, item);
                    }
                }

                var everyTypes = types.Where(type => typeof(IDependency).IsAssignableFrom(type) && !type.IsAbstract);
                foreach (var item in everyTypes)
                {
                    var interFaces = item.GetInterfaces();
                    foreach (var interFace in interFaces)
                    {
                        services.AddTransient(interFace, item);
                    }
                }
            }
        }
Exemple #2
0
        private static void RegisterHandle(ContainerBuilder builder, string startWithName, Assembly executingAssembly = null)
        {
            if (executingAssembly == null)
            {
                executingAssembly = Assembly.GetExecutingAssembly();
            }

            var assemblies = AssemblyFinder.Instance(startWithName).FindAll().Union(new[] { executingAssembly }).ToArray();

            builder.RegisterAssemblyTypes(assemblies)
            .Where(type => typeof(ILifetimeDependency).IsAssignableFrom(type) && !type.IsAbstract)
            .AsSelf()                  //自身服务,用于没有接口的类
            .AsImplementedInterfaces() //接口服务
                                       //.PropertiesAutowired()//属性注入
            .InstancePerLifetimeScope();

            builder.RegisterAssemblyTypes(assemblies)
            .Where(type => typeof(IRequestDependency).IsAssignableFrom(type) && !type.IsAbstract)
            .AsSelf()                  //自身服务,用于没有接口的类
            .AsImplementedInterfaces() //接口服务
                                       //.PropertiesAutowired()//属性注入
            .InstancePerRequest();     //保证生命周期基于请求

            builder.RegisterAssemblyTypes(assemblies)
            .Where(type => typeof(ISingleInstanceDependency).IsAssignableFrom(type) && !type.IsAbstract)
            .AsSelf()                  //自身服务,用于没有接口的类
            .AsImplementedInterfaces() //接口服务
                                       //.PropertiesAutowired()//属性注入
            .SingleInstance();         //单例

            builder.RegisterAssemblyTypes(assemblies)
            .Where(type => typeof(IDependency).IsAssignableFrom(type) && !type.IsAbstract)
            .AsSelf()                   //自身服务,用于没有接口的类
                                        //.PropertiesAutowired()//属性注入
            .AsImplementedInterfaces(); //接口服务

            RegisterEventHandler?.Invoke(builder);
        }