简体   繁体   English

具有多个 DBContext 的存储库还是每个具有 DBContext 的多个存储库?

[英]Repository with multiple DBContexts or multiple repositories each with a DBContext?

I am creating a web API with a resource and its sub-resources.我正在使用资源及其子资源创建 web API。 Because the API will be hosted on multiple servers with varying database schemas (which I cannot change), I needed to create different implementations of the DBContext and create some logic in Startup.cs that handles context binding based on a property in appsettings.json .因为 API 将托管在具有不同数据库模式(我无法更改)的多个服务器上,所以我需要创建DBContext的不同实现并在Startup.cs中创建一些逻辑来处理基于appsettings.json中的属性的上下文绑定。 Some of the servers store the resource data on different databases, which means I have to use multiple DBContext for those servers' implementations.一些服务器将资源数据存储在不同的数据库中,这意味着我必须为这些服务器的实现使用多个DBContext

The question arises how should I structure the repositories and contexts in a way that minimizes code duplication while also keeping code bloat down?问题出现了,我应该如何以最小化代码重复同时保持代码膨胀的方式构建存储库和上下文?

Should I create a single repository for the resource and its sub-resources that holds multiple contexts?我应该为资源及其包含多个上下文的子资源创建一个存储库吗? If so, how do I ensure my app instantiates and binds these multiple contexts to a respository?如果是这样,我如何确保我的应用程序实例化这些多个上下文并将这些上下文绑定到存储库?

Should I create a separate repository for each resource with each holding its corresponding context?我应该为每个资源创建一个单独的存储库,每个资源都包含其相应的上下文吗?

Am I using the wrong pattern here or misusing the pattern?我在这里使用了错误的模式还是滥用了模式?

Most importantly, you must have defined abstractions for working with data.最重要的是,您必须定义处理数据的抽象。 These are interfaces.这些是接口。 A lot of small interfaces that follow the principles of SOLID.很多遵循SOLID原则的小接口。

For example:例如:

interface IUser
{
    User GetById(int id);
    void Save(User user);
}

Moreover, you can go further and break this interface into even smaller ones:此外,您可以进一步 go 将此接口分解为更小的接口:

interface IGetUser
{
    User GetById(int id);
}

interface ISaveUser
{
    void Save(User user);
}

This is instead of one huge IRepository interface, with dozens and hundreds of methods.这不是一个巨大的IRepository接口,有几十个和几百个方法。

Then you use these interfaces in your code without much thinking about how they are implemented.然后,您在代码中使用这些接口,而无需过多考虑它们是如何实现的。

Now you can use a mini-repository like this in one place:现在您可以在一个地方使用这样的迷你存储库:

class FirstUserRepository : IUser
{
    private FirstContext _context;

    // here constructor

    public User GetById(int id)
    {
        return _context.Users.FirstOrDefault(u => u.Id == id);
    }
    // ...
}

And such in another place:在另一个地方是这样的:

class SecondUserRepository : IUser
{
    private SecondContext _context;
    //...
}

Accordingly, the necessary context implementations are created, linked to different databases.因此,创建了必要的上下文实现,链接到不同的数据库。

// Connect to a one database
class FirstContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Customer> Customers { get; set; }
}

// Connect to an another database
class SecondContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Product> Products { get; set; }
}

And so on.等等。

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

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