I am trying to build a Domain Model that is completly isolated from the Data Model after reading articles like this: http://blog.sapiensworks.com/post/2012/04/07/Just-Stop-It!-The-Domain-Model-Is-Not-The-Persistence-Model.aspx. There are only six of us working on this system at the moment, however this could increase in future to 9+. However, I am beginning to think that this is not the right approach. I read a lot of questions on here, which appear to tell me to map the ORM directly to the Domain Model.
I recently asked this question: “Should the Data Model be identical to the domain model for mapping purposes?”. One of the answerers says: “I believe the mapping in between both should be within a (persistence-oriented) repository”. How do you do this mapping? I don’t believe I should be using AutoMapper in the repository because of the reasons stated here: https://stackoverflow.com/questions/20944865/repository-pattern-and-mapping-between-domain-models-and-entity-framework and here: http://enterprisecraftsmanship.com/2016/02/08/specification-pattern-c-implementation/ i.e. I cannot simply do this in the repository:
public Customer getId() { CustomerData customerData = customerRepository.getById(id); return Mapper.Map<CustomerDomain>(customerData); }
I cannot do this because the invariants of the domain object will not be considered. How can I return a domain object from the repository. Do I inject a factory into the repository? Is this even the right approach or is there another pattern for mapping data objects to domain objects?