简体   繁体   English

ASP.NET MVC应用程序中的多租户

[英]Multi Tenancy in ASP.NET MVC Application

In the following example, what is an efficient way to implement multitenancy with minimal redundant code? 在下面的示例中,用最少的冗余代码实现多租户的有效方法是什么? In this scenario, tenant is a student. 在这种情况下,租户是一名学生。 The students' school has 2 locations and each location is required to have the data (ie, Courses) stored in a seperate database. 学生所在的学校有2个地点,每个地点都需要将数据(即课程)存储在单独的数据库中。 When a student logs in, their location determines which database to pull from. 学生登录时,他们的位置决定了要从哪个数据库中提取。

I'm using Entity Framework and Repository Pattern. 我正在使用实体框架和存储库模式。 Currently I have the implementation for accessing Location 1 DB. 目前,我已经实现了访问位置1数据库的实现。 I've looked into different options to implement Location 2, such as injecting a TenantContext in the HomeController contructor, but I am stuck on how to set the correct database connection and what approach would be most efficient. 我已经研究了实现位置2的不同选项,例如在HomeController构造函数中注入TenantContext,但是我一直在坚持如何设置正确的数据库连接以及哪种方法最有效。

Below is the code for Location 1 only. 以下是仅位置1的代码。

Example Controller 控制器示例

public class HomeController : Controller
{
    ICourseRepository courseRepository;

    //How to set the correct repository to use based on location?
    public HomeController(ICourseRepository courseRepository)
    {
        this.courseRepository = courseRepository;
    }

    //Register for a new class
    public ViewResult Register()
    {
        var courseList = courseRepository.AvailableCourses();
        return View(courseList);
    }
}

CourseRepository 课程资料库

public class CourseRepository : ICourseRepository
{
    private Location1DB context = new Location1DB();

    public List<Course> AvailableCourses()
    {
        //Get available courses from Location 1 Course Table
    }
}

Location1Model.Context.cs (This is genereated using EF DbContext Generator) Location1Model.Context.cs (使用EF DbContext Generator生成)

public partial class Location1DB: DbContext
{
    public Location1DB()
        : base("name=Location1DB")
    {
    }

    public DbSet<Course> Courses { get; set; }
}

Web.config Web.config

<connectionStrings>
     <add name="Location1DB" ... />
     <add name="Location2DB" ... />
</connectionStrings>

sorry I'll try to edit and provide a more complete answer when I get time tomorrow, but check this SO answer and the castle windsor container doco to get you started (also not sure what container you are using but all the major ones should have this functionality). 抱歉,明天我有时间时,我会尝试编辑并提供更完整的答案,但是请检查此答案以及Castle Windsor集装箱doco以使您入门(也不确定您使用的是哪个集装箱,但所有主要的集装箱都应该有)此功能)。

The thought would be to read the LocationID out of the cookie, grab the right connection string and have the container pass it as a parameter to your dbcontext. 想法是从cookie中读取LocationID,获取正确的连接字符串,并使容器将其作为参数传递给dbcontext。

DbContext is a partial class remember, so you could declare your own partial DbContext class, put an interface on it and pass that as a dependency into your repository. 记住DbContext是一个局部类,因此您可以声明自己的局部DbContext类,在其上放置一个接口,并将其作为依赖项传递到存储库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM