Automatic injection in ASP.NET Core, a batch injection

When we use AddScoped, AddTransient, AddSingleton of these methods it is troublesome. We each additional interface and when its implementation is not required here registered a hard-coded line of code do? Fortunately, after a small project, but when we become a huge project, where the dependency injection how to maintain it?
In turn online for a long time, seen a lot of method implementation code is not very elegant feel, think, or to write a more practical now, we only need to define and implement interfaces in accordance with a prescribed. Application can automatically scan and register these interfaces and implementation class corresponding to the assembly, to complete the automatic registration dependency injection, specific implementations may be realized by interfaces or characteristic, if necessary to distinguish AddScoped, AddTransient, AddSingleton can define different interfaces to achieve, where do we define an interface IDenpendency presentations, specific to achieve the following: 

Step 1: Create a space such as an interface

public  interface IDenpendency {}

Step 2: Create functional interfaces need to be injected, these interfaces is what you want to achieve some of the features of the package, they are inherited interfaces IDenpendency first step in creating, as I create a functional interface

public interface IUserService:IDenpendency
{
  //function
}

The third step: to achieve functional interfaces need to inject follows, I created a class that implements the UserService

public  class UserService: IUserService 
{
  // function is embodied
}

 Step Four: Extended IServiceCollection batch injection method AddDataService

    public static class DataServiceExtension
    {
        /// <summary>
        /// 注入数据
        /// </summary>
        /// <param name="services"></param>
        public static IServiceCollection AddDataService(this IServiceCollection services)
        {
            #region 依赖注入
            //services.AddScoped<IUserService, UserService>();
            var baseType = typeof(IDependency);
            var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
            var referencedAssemblies = System.IO.Directory.GetFiles(path, "*.dll").Select(Assembly.LoadFrom).ToArray();
            var types = referencedAssemblies
                .SelectMany(a => a.DefinedTypes)
                .Select(type => type.AsType())
                .Where(x => x != baseType && baseType.IsAssignableFrom(x)).ToArray();
            var implementTypes = types.Where(x => x.IsClass).ToArray();
            var interfaceTypes = types.Where(x => x.IsInterface).ToArray();
            foreach (var implementType in implementTypes)
            {
                var interfaceType = interfaceTypes.FirstOrDefault(x => x.IsAssignableFrom(implementType));
                if (interfaceType != null)
                    services.AddScoped(interfaceType, implementType);
            }
            
            #endregion

            return services;
        }
    }
View Code

Step four: Call AddDataService batch injection method in Startup.cs

 

 

 

 My original article can be freely reproduced for non-commercial purposes, reproduced leave the original source 

Guess you like

Origin www.cnblogs.com/hjpms/p/11108915.html