How is the Data Mapper pattern different from the Repository Pattern?

一世执手 提交于 2019-12-02 15:13:32
Andrei

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

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.

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.

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!