Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 848
John Smith Avatar asked Nov 25 '12 11:11

John Smith


People also ask

Can we use dto in repository?

The DTO Layer Created by the Service Wizard The DTO layer itself is designed to be highly decoupled, so that the generated assemblers, DTOs, repositories and even services can be easily used outside of the generated service.

What is a repository pattern?

The Repository pattern. Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer.

What is Data Mapper in c#?

The AutoMapper in C# is a mapper between two objects. That is AutoMapper is an object-object mapper. It maps the properties of two different objects by transforming the input object of one type to the output object of another type.

What is Data Mapper in Java?

The Data Mapper is a layer of software that separates the in-memory objects from the database. Its responsibility is to transfer data between the two and also to isolate them from each other.


1 Answers

[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'.

like image 139
Andrei Avatar answered Oct 29 '22 21:10

Andrei