[英]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: 但是,这两种实现都有不同的作用:
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.