-3

With asp.net-core service injection如何添加multi-tenant unitofwork(包含多个存储库)?c#netcore 1.1 unitofwork multitenant

在我的代码现在,我有一个SetTenantId功能,在每个存储库更新tenantId

public class ContinentsController : Controller, ICustomApiController<Continent> 
{ 
    private readonly IUnitOfWork _data; 

    public ContinentsController(IUnitOfWork unitOfWork) 
    { 
     _data = unitOfWork; 
     _data.SetTenantId(currentUserTenantId); 
    } 
} 

public interface IUnitOfWork : IDisposable 
{ 
    ITicketRepository Tickets { get; } 
    int Save(); 
    void SetTenantId(int tenantId); 
} 

有没有其他的解决办法?

+0

只要使用谷歌...所有你需要的是阅读的测试: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection –

回答

0

您可以创建一个服务层,该服务层将从您的控制器中调用,以便对数据执行任何CRUD操作。在这种情况下,您将传递没有提供给服务方法的租户标识符。

服务方法将传递租户标识符2工作单元或存储库。在仓库中,您可以拥有类似已有的方法,这些方法将为选择查询返回IQueryable,以便为租户标识符进行筛选。

例如:获取每个租户标识符的单个数据记录。

context.AutoTenantFilter(). FirstorDefault();

对于搜索方法的实现看起来像下面 context.AutoTenantFilter(). Where(x=>x.id==condition.value);

这个模型能够通过嘲讽