Peppermintology
Peppermintology

Reputation: 10210

Mapping objects between the domain and database

When mapping between database objects and domain objects, which "layer" of my application should this functionality reside in?

Say I have:

Where is the mapping more appropriately placed in this scenario?

Upvotes: 2

Views: 236

Answers (2)

Gabriel Aramburu
Gabriel Aramburu

Reputation: 2981

If you want to follow the principle that says a layer only need to know about the next layer down (idea taken from the Law of Demeter) the best practice would be to place the translation code in the Service Layer.

However, as the core domain could be considered itself another layer in the middle of your service and data access layer, and considering the service layer talks directly with the DAL (breaking the LoD principle), probably the simpler and more pragmatic solution (as @wblanks says) could be to put the translation code in the data access layer.

Upvotes: 0

wblanks
wblanks

Reputation: 598

Your DAL would be a good place in this scenario, as it already references your core where your domain entities should live. It can do the fetching of data, and convert it to domain objects before returning.

In this way you can encapsulate the knowledge of the DB objects to the DAL layer, which is a good thing. If you ever need to change your DB out, you only need to modify this layer. The rest of the application only knows about the Domain.

Upvotes: 1

Related Questions