提示:一般情况下我们都是使用一个接口一个实现类,但是有一些情况,我们为了实现多态,我们会定义一个接口,多个实现类。这种情况我们在NetCore自带的依赖注入容器中,我们应该怎么来实现呢?
目录
一般情况下我们都是使用一个接口一个实现类,但是有一些情况,我们为了实现多态,我们会定义一个接口,多个实现类。这种情况我们在NetCore自带的依赖注入容器中,我们应该怎么来实现呢?
依赖注入(Dependency Injection),是这样一个过程:由于某客户类只依赖于服务类的一个接口,而不依赖于具体服务类,所以客户类只定义一个注入点。在程序运行过程中,客户类不直接实例化具体服务类实例,而是客户类的运行上下文环境或专门组件负责实例化服务类,然后将其注入到客户类中,保证客户类的正常运行。
代码如下(示例):
- using JuCheap.WebApi.Common;
- using JuCheap.WebApi.TenantCustomServices;
- using JuCheap.WebApi.TenantFactory;
- using JuCheap.WebApi.TenantServices;
- using JuCheap.WebApi.Utils;
- using Microsoft.Extensions.DependencyInjection;
- using System.Linq;
- using System.Reflection;
-
- namespace JuCheap.WebApi
- {
- /// <summary>
- /// 模块初始化
- /// </summary>
- public static class JuCheapServiceRegistor
- {
- public static void AddJuCheapService(this IServiceCollection services)
- {
- //基础服务注册
- services.AddScoped<ITenantFactoryService, TenantFactoryService>();
- services.AddScoped<IRedisService, RedisService>();
- services.AddScoped<ITenantServiceBase, TenantServiceBase>();
-
- //批量注入租户级自定义服务
- var serviceRegistrations = typeof(TenantServiceBase).Assembly.GetTypes()
- .Where(type => type.Namespace != null
- && type.Namespace.StartsWith("JuCheap.WebApi.TenantCustomServices")
- && type.GetCustomAttributes<TenantAttribute>().Any(x => x.TenantIds != null && x.TenantIds.Any()))
- .Select(type => new { Implementation = type })
- .ToList();
- foreach (var service in serviceRegistrations)
- {
- services.AddScoped(service.Implementation);
- }
- }
- }
- }
代码如下(示例):
- using JuCheap.Common;
- using JuCheap.Models;
- using JuCheap.TenantServices;
- using JuCheap.Utils;
-
- namespace JuCheap.TenantCustomServices
- {
- /// <summary>
- /// 租户默认服务
- /// </summary>
- public class TenantServiceBase : ITenantServiceBase
- {
- public virtual string GetDefaultParam(int tenantId)
- {
- return $"{tenantId}-test";
- }
- }
-
- /// <summary>
- /// 租户默认服务接口
- /// </summary>
- public interface ITenantServiceBase
- {
- string GetDefaultParam(int tenantId);
- }
- }
-
-
默认服务实现了一个获取默认参数的接口,但是我们不同的用户,获取的参数有可能不一样,有可能做过定制开发等等。
- using JuCheap.Common;
- using JuCheap.Models;
- using JuCheap.TenantServices;
- using JuCheap.Utils;
-
- namespace JuCheap.TenantCustomServices
- {
- /// <summary>
- /// 用户扩展服务
- /// </summary>
- [Tenant(123456)]
- public class TenantService123456 : TenantServiceBase
- {
- public override string GetDefaultParam(int tenantId)
- {
- return $"{tenantId}-custom-123456";
- }
- }
- }
默认服务,我们可以通过直接注入ITenantServiceBase接口,就可以使用了,但是我们做过用户扩展服务的,应该怎么使用?我们需要一个工厂的服务接口,如下:
- using JuCheap.Common;
- using JuCheap.TenantServices;
- using Microsoft.Extensions.DependencyInjection;
- using System;
- using System.Linq;
- using System.Reflection;
-
- namespace JuCheap.TenantFactory
- {
- /// <summary>
- /// 租户服务获取工厂构造器
- /// </summary>
- public interface ITenantFactoryService
- {
- /// <summary>
- /// 获取租户服务,如果没有自定义服务,则返回默认的TenantBaseService服务
- /// </summary>
- /// <param name="tenantId">租户Id</param>
- /// <returns></returns>
- ITenantServiceBase GetTenantService(int tenantId);
- }
-
- /// <summary>
- /// 用户服务获取工厂构造器
- /// </summary>
- public class TenantFactoryService : ITenantFactoryService
- {
- private readonly IServiceProvider _serviceProvider;
-
- public TenantFactoryService(IServiceProvider serviceProvider)
- {
- _serviceProvider = serviceProvider;
- }
-
- /// <summary>
- /// 获取租户服务,如果没有自定义服务,则返回默认的TenantBaseService服务
- /// </summary>
- /// <param name="tenantId">租户Id</param>
- /// <returns></returns>
- public ITenantServiceBase GetTenantService(int tenantId)
- {
- var serviceRegistrations = typeof(TenantServiceBase).Assembly.GetTypes()
- .Where(type => type.Namespace != null
- && type.Namespace.StartsWith("JuCheap.TenantCustomServices")
- && type.GetCustomAttributes<TenantAttribute>().Any(x => x.TenantIds != null && x.TenantIds.Contains(tenantId)))
- .ToList();
- if (serviceRegistrations.Any())
- {
- if (serviceRegistrations.Count > 1)
- {
- throw new Exception($"{tenantId}的租户扩展服务找到多个实现类,请修改");
- }
- return _serviceProvider.GetRequiredService(serviceRegistrations.First()) as ITenantServiceBase;
- }
- //没有找到直接返回默认租户服务
- return _serviceProvider.GetRequiredService<ITenantServiceBase>();
- }
- }
- }
当我们的用户有扩展服务的是否,我们使用工厂服务接口来获取扩展服务,如下:
- using JuCheap.Models;
- using JuCheap.TenantFactory;
- using Microsoft.AspNetCore.Mvc;
-
- namespace JuCheap.Controllers
- {
- /// <summary>
- /// 账号绑定Api
- /// </summary>
- [Route("api/[controller]/[action]")]
- [ApiController]
- public class BindAccountController : ControllerBase
- {
- private readonly ITenantFactoryService _tenantFactoryService;
-
- public BindAccountController(ITenantFactoryService tenantFactoryService)
- {
- _tenantFactoryService = tenantFactoryService;
- }
-
- /// <summary>
- /// 执行绑定验证
- /// </summary>
- [HttpPost]
- public IActionResult Bind([FromBody] BindRequestDTO requestDTO)
- {
- var tenantService = _tenantFactoryService.GetTenantService(User.TenantId);
- tenantService.BindAccount(requestDTO);
- return Ok(true);
- }
- }
- }
netcore已经为我们提供了丰富多样的服务注入方式,类似上面的注入方式,我们也可以提供一个Func<int, ITenantServiceBase>的函数来实现,欢迎大家一起讨论。