How is the Data Mapper pattern different from the Repository Pattern?
Asked Answered
A

1

34

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

Algae answered 25/11, 2012 at 11:43 Comment(2)
If your data mapper is too big or handles large amount of querying than actually mapping objects, you introduce another class to concentrate on querying part alone , which you name it as a repository pattern.Ithaman
It's rare that you benefit from the repository layer. That links say repository may help some complex systems.Goldsworthy
M
47

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

Megathere answered 25/11, 2012 at 12:17 Comment(6)
Nice explanation, can you give an example of how these may differ. From what I see in Java, .NET and PHP frameworks, the two concepts seem to be used interchangeably and it's very confusing.Allrud
@LordYggdrasill If you have a specific issue with a particular framework you should present it in the form of a question on this site. Querying and mapping are logically different tasks, but their implementations are often tightly coupled, which may explain why some (citation needed) may choose to consider them together.Megathere
Well here I do have an example from ASP.NET's website. As you can see, their implementation of Repository pattern looks exactly the same as Martin Fowler's Data Mapper in POEAA. Do you know how to explain this phenomenon? Maybe ASP.NET's repository is in fact a data mapper? asp.net/mvc/overview/older-versions/…Allrud
@LordYggdrasill The repository handles querying. It doesn't mean that if a class does something other than querying it's not a repository, but rather that if a class provides querying then it is a repository. In the example link the data mapping part is delegated to the DbSet. Fowler's writings are not scripture, you can choose to ignore them or interpret them as best suits your application (though you shouldn't). Please post a separate question if you have other issues, the comment section of a 2 year old question is not the place for discussions.Megathere
I see, thanks for your answer. So in ASP.NET MVC, the DBSet are actually Data Mappers? And Repository handles not only mapping data into domain objects, but also relationships such as dependency? Well I did post a new question before, but no one was answering. Thats why I asked in this one since it did have a response: #27996619Allrud
What I don't get is who is responsible for the resolution of relations. Say I want to fetch a List<Subscription> and each Subscription has a List<AddOn>. I could fetch all that data in the repository layer and return it but wouldn't the repository then perform work that's actually job of the mapping layer?Electrocautery

© 2022 - 2024 — McMap. All rights reserved.