简体   繁体   English

单个界面背后的不同数据库存储库实现

[英]Different database repository implementations behind single interface

I am working on an application for which I need to write a data access layer. 我正在开发一个需要为其编写数据访问层的应用程序。 My first thought was to create a bunch of repositories for each entity that I need but I am now facing the a challenge with separating the different actions of a repository. 我的第一个想法是为我需要的每个实体创建一堆存储库,但是现在我面临着将存储库的不同操作分开的挑战。 I would like to hear your thoughts on this. 我想听听您对此的想法。

First, I would like to support two types of communication to the database - direct sql queries (for example, insert into, update, etc) and bulk inserts like loading data from a file (or other source). 首先,我想支持两种与数据库的通信-直接sql查询(例如,insert,update等)和批量插入,例如从文件(或其他来源)加载数据。 However, both of these implementation do different things: 但是,这两种实现都有不同的作用:

  • The simple repository fires a query to SQL server 简单的存储库向SQL Server发出查询
  • The bulk repository first adds a record to a file or in memory. 批量存储库首先将记录添加到文件或内存中。 Once it is done processing all records, it synchronizes with the database. 完成所有记录的处理后,它将与数据库同步。

My first attempt at the class structure for this is: 我对此的类结构的首次尝试是:

  public class Product{

  }

  public interface IProductRepository {
    Product GetProduct(int id);
    void CreateProduct(Product p);
  }

  public class SqlProductRepository : IProductRepository
  {
    public Product GetProduct(int id)
    {
      throw new NotImplementedException();
    }

    public void CreateProduct(Product p)
    {
      throw new NotImplementedException();
    }   
  }

  public class BulkLoadRepository : IProductRepository
  {
    public Product GetProduct(int id)
    {
      throw new NotImplementedException();
    }

    public void CreateProduct(Product p)
    {
      throw new NotImplementedException();
    }    
  }

However, this structure is missing a synchronization function at the end for the bulk repository. 但是,此结构最后缺少批量存储库的同步功能。 If I do end up adding a Sync() function, I will need to leave it empty for the "simple" repository. 如果确实添加了Sync()函数,则需要在“简单”存储库中将其保留为空。

Any thoughts on how to support both functionalities but still hide them behind one interface? 关于如何支持这两种功能,但仍将它们隐藏在一个界面中的任何想法?

Thanks in advance! 提前致谢!

First of all, why are you creating an interface for each object type. 首先,为什么要为每种对象类型创建一个接口。 A repository would usually just have Get, Create, Update, Delete methods for example, maybe a generic Get... where the IRepository is also generic. 例如,存储库通常仅具有Get,Create,Update,Delete方法,也许是通用的Get ...,其中IRepository也是通用的。

You could have a second interface inheriting IRepository which has the Sync method and only the Bulk repository implements that one. 您可以拥有第二个继承IRepository接口,该接口具有Sync方法,并且只有Bulk存储库才实现该接口。 In this case you can still access both repositories methods defined by IProductRepository` 在这种情况下,您仍然可以访问IProductRepository` methods defined by两个存储库methods defined by

Or have a base class for Bulk repositories which implements/defines sync. 或具有用于实现/定义同步的Bulk存储库的基类。

I'm not sure why you would leave it empty for the simple repository. 我不确定为什么将简单存储库留空。 Sync == Commit in most of my repositories. 同步==在我的大多数存储库中提交。 Although more precisely this is the UnitOfWork pattern, not the repository pattern. 尽管更确切地说,这是UnitOfWork模式,而不是存储库模式。

Exposing a separate commit method is very common for transactional systems. 对于事务系统,公开单独的提交方法非常普遍。

Repository and Unit of Work patterns - How to save changes 存储库和工作单元模式-如何保存更改

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

相关问题 测试单个接口的不同具体实现? - Testing different concrete implementations of single interface? 如何确定要为接口的不同实现调用哪个存储库? - How to figure out which repository to call for different implementations of an interface? 具有不同类型的接口的实现? - Implementations of an Interface with Different Types? 使用单个接口注册多个实现 - Register multiple implementations with single interface DI:不同控制器的相同接口的不同实现 - DI: Different implementations of the same interface for the different controllers 使用Autofac将接口的不同实现注入到不同的控制器中 - Injecting different implementations of an interface into different controllers with Autofac 在单个测试类中测试接口的多个实现 - Testing multiple implementations of an interface in a single test class 单个类上同一接口的多种实现的替代方法 - Alternatives to multiple implementations of the same interface on a single class IoC - 单一接口的多种实现支持 - IoC - Multiple implementations support for a single interface 混合不同数据源的存储库实现 - Mixing Repository implementations for different data sources
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM