I found two patterns which appear to have the same goal - what is the difference?
[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'.