在ASP.NET MVC注射的多租户存储库与StructureMap [英] Injecting multi-tenant repositories with StructureMap in ASP.NET MVC

查看:174
本文介绍了在ASP.NET MVC注射的多租户存储库与StructureMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多租户ASP.NET MVC应用程序注入我的房客的情况下实施StructureMap 的检索基于一个数据 ITenantContext 接口。在租户的问题是从的RouteData 中的基本控制器的 OnActionExecuting

I'm implementing StructureMap in a multi-tenant ASP.NET MVC application to inject instances of my tenant repositories that retrieve data based on an ITenantContext interface. The Tenant in question is determined from RouteData in a base controller's OnActionExecuting.

我如何告诉StructureMap构建 TenantContext(tenantID); 其中tenantID源自我的的RouteData 或一些基本控制器的财产?

How do I tell StructureMap to construct TenantContext(tenantID); where tenantID is derived from my RouteData or some base controller property?

由于以下路线:

{tenant}/{controller}/{action}/{id}

我的基本控制器检索和存储正确的租户基础上,{}房客URL参数。使用租户,具有存储库 ITenantContext 可以构造只提取是有关该租户的数据。

My base controller retrieves and stores the correct Tenant based on the {tenant} URL parameter. Using Tenant, a repository with an ITenantContext can be constructed to retrieve only data that is relevant to that tenant.

根据其他DI的问题,可能会 AbstractFactory 是一个解决方案?

Based on the other DI questions, could AbstractFactory be a solution?

推荐答案

不要租户存储在控制器上,因为它不会提供给注射服务,因为你发现了。创建一个薄服务,它的唯一责任是确定租户标识符。该服务可以直接访问静态和HttpContext的。这个类并不真正需要的是单元测试的 - 它的目的是使其他类可测试到系统的其他部分隔离开。

Do not store the tenant on the controller, as it will not be available to injected services, as you discovered. Create a thin service whose sole responsibility is to determine the tenant identifier. The service can access statics and HttpContext directly. This class doesn't really need to be unit-testable - its purpose is to isolate the rest of the system so that other classes are testable.

如果你想 ITenantContext 是该服务,它可能看起来是这样的:

If you want ITenantContext to be that service, it could look something like:

public class TenantContext : ITenantContext
{
    public string GetTenant()
    {
        var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current));
        return routeData.GetRequiredString("tenant");
    }
}

现在您的控制器只需要你的资料库界面的依赖关系,你的仓库实现(即一个关心任何其他服务)可以依赖于 ITenantContext 。该控制器并不需要了解租户。

Now your controller can just have a dependency on your repository interface, and your repository implementation (an any other services that care) can depend on ITenantContext. The controller doesn't need to know about tenants.

这篇关于在ASP.NET MVC注射的多租户存储库与StructureMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆