0

I'm using the data mapper pattern in Java for accessing the database. Is it OK to call a mapper inside of another mapper? As far as I'm concerned, mappers should work on their own without a dependency on other mappers, but it seems that someone else who works on the same project with me has a different opinion.

To give an example: I have a Customer object and a ContactPerson object. The Customer has a ContactPerson object in it as a field. For getting data from a database, I have both a CustomerMapper and a ContactPersonMapper. When retrieving the Customer data from the database, I need to get its ContactPerson data at the same time. Is it a good idea to use the ContactPerson mapper inside my CustomerMapper, or should I make the mappers completely independent of each other?

ZimZim
  • 3,291
  • 10
  • 49
  • 67

1 Answers1

1

I guess you are using something similar to spring jdbctemplate and mappers here. I use that pattern a lot, and I tend to agree with you. I never call a second mapper inside a mapper. In my opinion the code is much easier to both test and read when the mappers are standalone. I prefer to have as little logic as possible in the DAOs, and leave logic to other code levels.

A few times in the past I made logic inside mappers to map structures to more advanced objects, but I have moved away from even doing that. I think the best apporach is to make the mappers very, very simple.

Vegard
  • 1,802
  • 2
  • 21
  • 34
  • I agree with this. When I first saw the data mapper pattern, I immediately thought that it was implied that the mappers were simply stand-alone. I think if someone alters a mapper which is being used inside another mapper, a big code malfunction can arise. Thanks for the answer. I'm hoping for this to be confirmed by other people as well. – ZimZim May 09 '13 at 08:28