我可以使用全局过滤器处理不同的多租户吗?

发布于 2024-10-21 09:02:45 字数 503 浏览 2 评论 0原文

我有一个 ASP.NET MVC 应用程序,它已实现会员资格。 因此,用户必须登录。每个用户都属于一个组织(多租户)。

我将如何全局处理组织参数?我认为这对于全局过滤器来说可能是一件好事,因为需要为给定的组织过滤所有数据。并且组织与用户名关联但不在同一个表中。

例如,我有一个像这样的索引操作,

public ActionResult Index()
{
var result = _repository.GetListByOrganisation(organisation);
return View(result);
}

我正在考虑有一个全局属性,该属性根据给定的用户名查询数据库中的组织。由于我的控制器已经包含授权属性,因此我有用户名。最好缓存组织(会话、控制器上下文),而不是在每个请求上从数据库查询组织。

这是实现类似事情的方法吗?或者还有其他更好的方法吗?如何从过滤器中设置控制器/控制器上下文的属性?

所以对此的任何想法和评论都会很棒......

I have an asp.net mvc app which has membership implemented.
So a user has to log in. Each user belongs to a organisation (multi-tenancy).

How would I handle the organisation parameter globaly? I was thinking this could be a good thing for a global filter because all the data needs to be filtered for the given organisation. And the organisation is connected with the username but not in the same table.

for example I have a index action like this

public ActionResult Index()
{
var result = _repository.GetListByOrganisation(organisation);
return View(result);
}

I was thinking about having a global attribute thats queries the db for an organisation based on a giving username. Since my controller already contains the authorize attribute I have the user name. It would be nice to cache the organisation (session, controllercontext) and not query the organisation from db on each request.

Is this a way to implement something like this? Or are there other ways which would be better? And how can I set a property on the controller / controllercontext from whithin a filter?

So any thoughts on this as well as comments would be great...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生活了然无味 2024-10-28 09:02:45

我会通过 DI 来完成此操作。

您可以使用第三方 DI 容器或您自己的代码。无论哪种方式,您都希望根据每个请求设置组织 ID。

因此,您将创建一个工作单元并将其注入控制器中。为了简单起见,我们假设您的工作单元是示例代码中的 _repository 字段,尽管大多数实际应用都更为复杂。

您向控制器添加构造函数参数:

public FooController(IFooRepository repository)
{
    this._repository = repository;
}

...以及 FooRepository 上的组织 ID:

public class FooRepository: IFooRepository
{
    public FooRepository(long organizationId)
    {
        this._organizationId = organizationId;
    }
}

现在,在您的 DI 容器设置或 MVC 控制器工厂中,您可以将这一切设置完毕:

builder.Register(c => new FooRepository(GetOrgIdForCurrentUser()).As<IFooRepository>();
builder.Register(c => new FooController(c.Resolve<IFooRepository>());

I would do this via DI.

You can use either a third-party DI container or your own code. Either way, you want to set the organization ID on a per-request basis.

So you'll be creating a unit of work and injecting that in your controller. For the sake of simplicity, let's pretend that your unit of work is the _repository field in your sample code, even though most real-world apps are more complex.

You add a constructor parameter to the controller:

public FooController(IFooRepository repository)
{
    this._repository = repository;
}

...and an organization ID on FooRepository:

public class FooRepository: IFooRepository
{
    public FooRepository(long organizationId)
    {
        this._organizationId = organizationId;
    }
}

Now in either your DI container setup or a MVC controller factory, you set this all up:

builder.Register(c => new FooRepository(GetOrgIdForCurrentUser()).As<IFooRepository>();
builder.Register(c => new FooController(c.Resolve<IFooRepository>());
初相遇 2024-10-28 09:02:45

也许您可以将组织嵌入到 URL 中,例如,如果您的路线如下所示 /{organization}/{controller}/{action}

那么您将获得类似

  • /bigorg/products/list
  • /smallorg/products/list 的

URL并且您将在控制器中接收组织,该组织可以是您的方法的参数,也可以通过 RouteData 对象接收。

Perhaps you could have the organization embedded on the URL, for example if your route looks like this /{organization}/{controller}/{action}

then you'll get URLs like

  • /bigorg/products/list
  • /smallorg/products/list

and you'll receive the organization in your controller either a parameter to your method or via the RouteData object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文