发布时间:2025-02-23 13:01
提示:一般情况下我们都是使用一个接口一个实现类,但是有一些情况,我们为了实现多态,我们会定义一个接口,多个实现类。这种情况我们在NetCore自带的依赖注入容器中,我们应该怎么来实现呢?
目录
前言
一、什么是依赖注入?
二、使用步骤
1.首先我们写一个扩展服务来批量注入我们的服务
2.默认实现服务TenantServiceBase
3.编写用户扩展服务
4.服务的使用
总结
一般情况下我们都是使用一个接口一个实现类,但是有一些情况,我们为了实现多态,我们会定义一个接口,多个实现类。这种情况我们在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
{
///
/// 模块初始化
///
public static class JuCheapServiceRegistor
{
public static void AddJuCheapService(this IServiceCollection services)
{
//基础服务注册
services.AddScoped();
services.AddScoped();
services.AddScoped();
//批量注入租户级自定义服务
var serviceRegistrations = typeof(TenantServiceBase).Assembly.GetTypes()
.Where(type => type.Namespace != null
&& type.Namespace.StartsWith("JuCheap.WebApi.TenantCustomServices")
&& type.GetCustomAttributes().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
{
///
/// 租户默认服务
///
public class TenantServiceBase : ITenantServiceBase
{
public virtual string GetDefaultParam(int tenantId)
{
return $"{tenantId}-test";
}
}
///
/// 租户默认服务接口
///
public interface ITenantServiceBase
{
string GetDefaultParam(int tenantId);
}
}
默认服务实现了一个获取默认参数的接口,但是我们不同的用户,获取的参数有可能不一样,有可能做过定制开发等等。
using JuCheap.Common;
using JuCheap.Models;
using JuCheap.TenantServices;
using JuCheap.Utils;
namespace JuCheap.TenantCustomServices
{
///
/// 用户扩展服务
///
[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
{
///
/// 租户服务获取工厂构造器
///
public interface ITenantFactoryService
{
///
/// 获取租户服务,如果没有自定义服务,则返回默认的TenantBaseService服务
///
/// 租户Id
///
ITenantServiceBase GetTenantService(int tenantId);
}
///
/// 用户服务获取工厂构造器
///
public class TenantFactoryService : ITenantFactoryService
{
private readonly IServiceProvider _serviceProvider;
public TenantFactoryService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
///
/// 获取租户服务,如果没有自定义服务,则返回默认的TenantBaseService服务
///
/// 租户Id
///
public ITenantServiceBase GetTenantService(int tenantId)
{
var serviceRegistrations = typeof(TenantServiceBase).Assembly.GetTypes()
.Where(type => type.Namespace != null
&& type.Namespace.StartsWith("JuCheap.TenantCustomServices")
&& type.GetCustomAttributes().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();
}
}
}
当我们的用户有扩展服务的是否,我们使用工厂服务接口来获取扩展服务,如下:
using JuCheap.Models;
using JuCheap.TenantFactory;
using Microsoft.AspNetCore.Mvc;
namespace JuCheap.Controllers
{
///
/// 账号绑定Api
///
[Route("api/[controller]/[action]")]
[ApiController]
public class BindAccountController : ControllerBase
{
private readonly ITenantFactoryService _tenantFactoryService;
public BindAccountController(ITenantFactoryService tenantFactoryService)
{
_tenantFactoryService = tenantFactoryService;
}
///
/// 执行绑定验证
///
[HttpPost]
public IActionResult Bind([FromBody] BindRequestDTO requestDTO)
{
var tenantService = _tenantFactoryService.GetTenantService(User.TenantId);
tenantService.BindAccount(requestDTO);
return Ok(true);
}
}
}
netcore已经为我们提供了丰富多样的服务注入方式,类似上面的注入方式,我们也可以提供一个Func