数据映射器模式与存储库模式不同? [英] How is the Data Mapper pattern different from the Repository Pattern?

查看:150
本文介绍了数据映射器模式与存储库模式不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现两种模式似乎有相同的目标 - 有什么区别?

I found two patterns which appear to have the same goal - what is the difference?

http://martinfowler.com/eaaCatalog/dataMapper.html

http://martinfowler.com/eaaCatalog/repository.html

推荐答案


[存储库]是映射
层的另一层抽象层,查询构造代码集中在一起。

[the Repository is] another layer of abstraction over the mapping layer where query construction code is concentrated.

DataMapper 确保围栏的DB侧不需要了解业务逻辑的具体细节,以及业务对象如何将数据保存在内存中并且您的围栏的业务侧不需要知道数据的存储方式。

The DataMapper ensures the DB side of the fence doesn't need to know about the specifics of your business logic and how the data is kept in memory by your business objects and your business side of the fence doesn't need to know how the data is stored.

为了说明,考虑到您的数据作为一组表示每行表示商店中的一个项目。在内存方面,您可能希望将该信息保存为 StoreItem 的列表,但可以将其作为两个列表,一个用于存货的项目,另一个用于外部存储,库存商品。
它将作为处理一个列表和两个列表之间的转换的 DataMapper 的工作。

To illustrate, consider that your data is kept in the DB as a set of rows, say each row represent an item in your store. On the in-memory side, you might want to keep that information not as a list of StoreItem but as two lists, one for items which are in stock and another for out-of-stock items. It would be the DataMapper's job to handle the transition between one list and two lists.

您可以通过在围栏的业务侧添加其他对象的列表和继承来使事情复杂化。 DataMapper将不得不将该表示转换为关系数据库。

You can complicate things by adding lists of other objects and inheritance on the business side of the fence. The 'DataMapper' would have to translate to and from that representation to the relational DB.

存储库将SELECT * FROM table WHERE condition功能提供给业务方面。您提供一个过滤器,它将返回与该过滤器匹配的对象集合。

The 'Repository' provides the "SELECT * FROM table WHERE condition" functionality to the business side. You provide a filter and it will return a collection of objects that matches that filter.

简而言之:DataMapper处理单个对象,存储库处理对象集合,并扩展DataMapper提供的功能。

In short: the 'DataMapper' deals with single objects, the 'Repository' deals with collections of objects and expands upon the functionality provided by the 'DataMapper'.

这篇关于数据映射器模式与存储库模式不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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