数据映射器,表数据网关(网关),数据访问对象(DAO)和存储库模式之间有什么区别? [英] What is the difference between the Data Mapper, Table Data Gateway (Gateway), Data Access Object (DAO) and Repository patterns?

查看:158
本文介绍了数据映射器,表数据网关(网关),数据访问对象(DAO)和存储库模式之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提高自己的设计模式技能,并且很好奇这些模式之间的区别是什么?所有这些似乎都是同一回事–封装特定实体的数据库逻辑,因此调用代码不了解基础持久层.通过我的简要研究,所有这些方法通常都可以实现您的标准CRUD方法并抽象出特定于数据库的详细信息.

I'm trying to brush up on my design pattern skills, and I'm curious what are the differences between these patterns? All of them seem like they are the same thing - encapsulate the database logic for a specific entity so the calling code has no knowledge of the underlying persistence layer. From my brief research all of them typically implement your standard CRUD methods and abstract away the database-specific details.

除了命名约定(例如,CustomerMapper与CustomerDAO,CustomerGateway与CustomerRepository)以外,还有什么区别(如果有)?如果存在差异,您何时会选择一个?

Apart from naming conventions (e.g. CustomerMapper vs. CustomerDAO vs. CustomerGateway vs. CustomerRepository), what is the difference, if any? If there is a difference, when would you chose one over the other?

过去,我会编写类似于以下内容的代码(自然而然地简化了-我通常不会使用公共属性):

In the past I would write code similar to the following (simplified, naturally - I wouldn't normally use public properties):

public class Customer
{
    public long ID;
    public string FirstName;
    public string LastName;
    public string CompanyName;
}

public interface ICustomerGateway
{
    IList<Customer> GetAll();
    Customer GetCustomerByID(long id);
    bool AddNewCustomer(Customer customer);
    bool UpdateCustomer(Customer customer);
    bool DeleteCustomer(long id);
}

,并具有一个CustomerGateway类,该类为所有方法实现特定的数据库逻辑.有时,我不会使用接口,而是将CustomerGateway上的所有方法设为静态(我知道,这使得它的测试性较差),所以我可以这样称呼它:

and have a CustomerGateway class that implements the specific database logic for all of the methods. Sometimes I would not use an interface and make all of the methods on the CustomerGateway static (I know, I know, that makes it less testable) so I can call it like:

Customer cust = CustomerGateway.GetCustomerByID(42);

对于Data Mapper和Repository模式,这似乎是相同的原理; DAO模式(我想与Gateway相同吗?)似乎也鼓励特定于数据库的网关.

This seems to be the same principle for the Data Mapper and Repository patterns; the DAO pattern (which is the same thing as Gateway, I think?) also seems to encourage database-specific gateways.

我错过了什么吗?用3-4种不同的方法来做同样的事情似乎有点奇怪.

Am I missing something? It seems a little weird to have 3-4 different ways of doing the same exact thing.

推荐答案

您的示例字词; DataMapper,DAO,DataTableGateway和Repository都具有相似的目的(当我使用它时,我希望取回Customer对象),但是意图/含义和实现方式不同.

Your example terms; DataMapper, DAO, DataTableGateway and Repository, all have a similar purpose (when I use one, I expect to get back a Customer object), but different intent/meaning and resulting implementation.

存储库 类似于集合,但具有更复杂的查询功能" [ Evans,域驱动设计],可以视为内存外观中的对象" (

A Repository "acts like a collection, except with more elaborate querying capability" [Evans, Domain Driven Design] and may be considered as an "objects in memory facade" (Repository discussion)

DataMapper 在对象和数据库之间移动数据,同时保持它们彼此之间以及映射器自身之间的独立性" ( TableDataGateway 到数据库表的网关(封装对外部系统或资源的访问的对象.一个实例处理表中的所有行") ( Fowler,PoEAA,TableDataGateway )

DAO "将数据资源的客户端界面与其数据访问机制分离/使特定数据资源的访问API适应通用的客户端界面" 允许"数据访问机制以独立于使用数据的代码进行更改".(太阳蓝图)

存储库似乎非常通用,没有公开数据库交互的概念. DAO提供了一个接口,使使用不同的基础数据库实现成为可能. TableDataGateway特别是围绕单个表的薄包装器. DataMapper充当中介,使Model对象能够独立于数据库表示(随着时间的推移)发展.

Repository seems very generic, exposing no notion of database interaction. A DAO provides an interface enabling different underlying database implementations to be used. A TableDataGateway is specifically a thin wrapper around a single table. A DataMapper acts as an intermediary enabling the Model object to evolve independently of the database representation (over time).

这篇关于数据映射器,表数据网关(网关),数据访问对象(DAO)和存储库模式之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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