29

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

Alex Bitek
  • 6,273
  • 4
  • 44
  • 77
John Smith
  • 2,797
  • 5
  • 27
  • 30
  • 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. – Rockstart Nov 26 '12 at 06:06

1 Answers1

44

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

Seph
  • 976
  • 11
  • 20
Andrei
  • 4,592
  • 22
  • 28
  • 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. – Lord Yggdrasill Feb 21 '15 at 20:37
  • 1
    @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. – Andrei Feb 21 '15 at 21:59
  • 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? http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application – Lord Yggdrasill Feb 22 '15 at 06:45
  • @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. – Andrei Feb 22 '15 at 07:49
  • 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: http://stackoverflow.com/questions/27996119/what-exactly-is-the-difference-between-a-data-mapper-and-a-repository – Lord Yggdrasill Feb 22 '15 at 12:48
  • The last sentence summed it up for me. Thanks! – steadweb Mar 26 '15 at 11:51
  • 2
    What I don't get is *who* is responsible for the resolution of relations. Say I want to fetch a `List` and each `Subscription` has a `List`. 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? – Stefan Falk Aug 22 '16 at 21:29