.Net WebApi 实现批量注入

在使用AddScoped、AddTransient、AddSingleton这类进行依赖注入时,每增加一个接口和实现类时,都需要在startup下注册一条,是不是很麻烦呢?下面来看看怎么实现批量注入

1.新增ServiceExtention扩展类

    public static class ServiceExtention
    {
        /// <summary>
        /// 批量注入
        /// </summary>
        /// <param name="services"></param>
        /// <param name="interfaceAssembly"></param>
        /// <param name="implementAssembly"></param>
        public static void AddBatchScoped(this IServiceCollection services, Assembly interfaceAssembly, Assembly implementAssembly)
        {
            var interfaces = interfaceAssembly.GetTypes().Where(t => t.IsInterface);
            var implements = implementAssembly.GetTypes();
            foreach (var item in interfaces)
            {
                var type = implements.FirstOrDefault(x => item.IsAssignableFrom(x));
                if (type != null)
                {
                    services.AddScoped(item, type);
                }
            }
        }

        /// <summary>
        /// 批量注入
        /// </summary>
        /// <param name="services"></param>
        /// <param name="interfaceAssembly"></param>
        /// <param name="implementAssembly"></param>
        public static void AddBatchSingleton(this IServiceCollection services, Assembly interfaceAssembly, Assembly implementAssembly)
        {
            var interfaces = interfaceAssembly.GetTypes().Where(t => t.IsInterface);
            var implements = implementAssembly.GetTypes();
            foreach (var item in interfaces)
            {
                var type = implements.FirstOrDefault(x => item.IsAssignableFrom(x));
                if (type != null)
                {
                    services.AddSingleton(item, type);
                }
            }
        }

        /// <summary>
        /// 批量注入
        /// </summary>
        /// <param name="services"></param>
        /// <param name="interfaceAssembly"></param>
        /// <param name="implementAssembly"></param>
        public static void AddBatchTransient(this IServiceCollection services, Assembly interfaceAssembly, Assembly implementAssembly)
        {
            var interfaces = interfaceAssembly.GetTypes().Where(t => t.IsInterface);
            var implements = implementAssembly.GetTypes();
            foreach (var item in interfaces)
            {
                var type = implements.FirstOrDefault(x => item.IsAssignableFrom(x));
                if (type != null)
                {
                    services.AddTransient(item, type);
                }
            }
        }

    }

2.添加两个类库IWebDIService和WebDIService,并在类库下分别创建接口和实现类

namespace IWebDIService
{
    public interface IUserService
    {
        string Hello();
    }
}
namespace WebDIService
{
    public class UserService : IUserService
    {
        public string Hello()
        {
            return "Hello World";
        }
    }
}

3.在startup.cs下添加调用扩展方法

 services.AddBatchScoped(Assembly.Load("IWebDIService"), Assembly.Load("WebDIService")); 

4.在创建的controller下注入服务

    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;
        public UserController(IUserService userService)
        {
            _userService = userService;
        }

        [HttpGet]
        public object Hello()
        {
            string hello = _userService.Hello();

            return Ok();
        }

    }

 5.结果展示

 

posted @ 2022-05-25 10:24  Zeng。  阅读(415)  评论(0编辑  收藏  举报