Search code examples
n-tier-architecture

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:

  • DAL - has a reference to core domain
  • Service - has a reference to DAL and core domain

Where is the mapping more appropriately placed in this scenario?


Solution

  • 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.