MVC的处理多租户技术 [英] Handling Multi Tenancy on MVC

查看:128
本文介绍了MVC的处理多租户技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下您对我处理多租户方式的意见。 。我米使用MVC3(切换到MVC4)和EF作为我的后端我使用一个单一的应用程序,共享模式多租户下面是code:

I would like to ask your opinions regarding the way I handle MultiTenancy. I"m using MVC3 (switching to MVC4) and EF as my backend. I'm using a single app, shared schema MultiTenancy. Below is the code:

public abstract class Service<T> where T : Entity
{
    private Repository<T> _repo;
    public Service()
    {
        _repo = new Repository<T>();
    }

    public bool Authenticate(int id)
    {
        //var companyInfo = _authorizationRepository.GetApiKey(apiKey);
        int tenantId = 0; // replaced by companyInfo using repository
        var entity = _repo.GetQuery(tenantId).Where(e => e.Id == id).First();

        if (tenantId != entity.TenantId)
            throw new ArgumentException();

        return true;
    }
}

public class EmployeeService : Service<Employee>
{
    private EmployeeRepository employeeRepository;
    public EmployeeService()
    {
        employeeRepository = new EmployeeRepository();
    }

    public Employee GetEmployeeById(int employeeId)
    {
        this.Authenticate(employeeId);
        return employeeRepository.GetById(employeeId);
    }
}

public class Entity
{
    public int Id { get; set; }
    public int TenantId { get; set; }
}

当然DI将在那里还有但为了简单起见我在这里把他们赶走(暂时)。我使用泛型,因为我有在TenantId与将在课堂上传递正确的实体比较麻烦(感觉它脏)的服务层。我虎视眈眈地重新code此使用FilterAttributes,但我没有任何想法如何。你们如何处理你的多租户?是设计有一个我可能从长远来看遇到一些关键性的缺陷?如果您在使用FilterAttributes一些样本,这将是一个很大的帮助。

Of course DI will be there as well but for simplicity I removed them here (temporarily). I used Generics (feeling dirty about it) on the Service Layer since I'm having trouble on comparing the TenantId with the correct Entity that will be passed on the class. I'm eyeing to recode this using FilterAttributes but I dont have any idea how to. How do you guys handle your multitenancy? Is the design have some crucial flaws that I may encounter on the long run? If you have some samples using FilterAttributes that would be a big help.

谢谢!

推荐答案

我们正在此刻建设pretty大的多租户技术的Web应用程序。那么它不是那么简单,因为它似乎,但一旦你建立你的架构是直线前进。我们是深深的发展,但你可以检查出在nanocms开源的部分。codeplex.com(我们还没有上传数据库喷气对接,我们将在几天内)

We are building pretty big multi tenancy web app at the moment. Well it's not so simple as it seems but once you build your architecture it's straight forward. Our is deeply in development but you can check out the open source part in nanocms.codeplex.com (we haven't uploaded the db jet butt we will in few days)

由于这是一个pretty广泛的问题,我会试着总结了一些问题和解决方案。

Since this is a pretty wide question I'll try to summarize some problems and solutions.

首先,你需要确定租户为每个请求。我们有一个全球行动过滤器,解析URL,并将其与数据库中的数据进行比较。当然,你必须缓存,以便于数据库中没有调用的所有数据。你不能保存任何的在一个cookie或会话,因为用户可以在访问时多于一名租客。我建议你​​把这些数据用在了Htt prequest项目,这样你只有一个请求一次,但仍然有可用的数据始终。

First you need to identify tenant for each request. We have a global action filter that parses url and compares it with data in database. Of course you must cache all data so no calls to database are made. You must not save any of that in a cookie or session because user can visit more than one tenant at the time. I suggest that you put that data in HttpRequest Items so you do that only once in a request but still have that data available always.

有关验证用户必须是唯一的。如果你想给每个租户某些用户不同的权限,必须想。如果是这样,你必须写你的身份验证code,甚至属性,因此您可以在目前的租户检查他的角色。在我们的应用程序,当用户进行身份验证,我们创建会话对象。在对象,我们有检查租户的权利静态方法。

For authenticate users must be unique. You must think if you want to give some user different rights on each tenant. If so you must write your authenticate code and even attribute so you can check his role in current tenant. In our app when user authenticates we create session object. In object we have static methods that check for rights in tenant.

我建议你保持了Htt prequest项强类型。我们有:

I suggest you keep HttpRequest Items strongly typed. We have:

public static int TenantID {
    get { return System.Web.HttpContext.Current.Items.Contains("TenantID") ? Convert.ToInt32(System.Web.HttpContext.Current.Items["TenantID"]) : -1; }
    set {
        if (!System.Web.HttpContext.Current.Items.Contains("TenantID"))
            System.Web.HttpContext.Current.Items.Add("TenantID", value);
        System.Web.HttpContext.Current.Items["TenantID"] = value;
    }
}

这篇关于MVC的处理多租户技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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