数据映射器模式与存储库模式不同?

编程入门 行业动态 更新时间:2024-10-11 21:23:12
本文介绍了数据映射器模式与存储库模式不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

martinfowler/eaaCatalog/dataMapper.html

martinfowler/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'.

更多推荐

数据映射器模式与存储库模式不同?

本文发布于:2023-11-03 21:23:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556120.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   数据   映射器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!