I found two patterns which appear to have the same goal – what is the difference?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
DataMapperensures 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
StoreItembut 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’.