When I work with my understanding of a domain-driven project, I find that I have a rule that seems to work, although I would like to see if it is too crowded, and also want to see other perspectives of the same situation.
My question is: "When are the domain model and persistence model stored in separate objects?" My choice language is Java at the moment, and I am using Spring's data warehouse model.
I see three main answers to my question.
- Always use separate domain objects from save objects.
- Use separate domain objects only if the use of domain methods (behavior) of persistence objects is inexpedient.
- Use save objects as domain objects in all cases.
To ask questions about DDD, I found that I need to use the limited context example, since I still do not know enough about DDD to ask in a more abstract way.
Here is my illustrative limited context: let's say I have a law coding system with the following business rules:
- Each book law must be classified.
- Each law has an identifier with two parts, a codification number prefix, and a coding coaxification suffix. (Example: 100-0100, 599-2030).
- There are several legal jurisdictions that use the codification system of laws, and they should be able to create their own co-administrators, but the codification prefixes are global and should be the same in all jurisdictions to facilitate general comparability.
- encoding number prefixes are grouped into broad encoding categories. Coding categories have a range of numbers, for example 100-199, 200-299, 700-799, etc.
To express this limited context as a conservation model, I have the following:
table: codification fields: chart_code, prefix, coassign, codification_category table: codification_chart fields: chart_code, jurisdiction_description table: codification_category fields: category, low_category_number, high_category_number, description table: global_codification fields: prefix, coassign, codification_category
I know, first I have to start with a domain model. I have a persistence model and a domain model
In my domain model, I have three domain objects
public Codification { private String prefix, coassign; codificationCategory codificationCaegory; // an enum type public Codification(...) { // set private vars } // getters for private variables } public CodificationChart { private List<Codification> chartCodifications = new ArrayList<>(); private String chartCode; // public constructor to initialize private variables // getters for private variables public Codification addCodificationToChart(Codification){...} public void removeCodificationFromChart(Codification){...} public boolean checkCodificationInChart(Codification){...} } public enum CodificationCategory { CIVIL, CRIMINAL, PROPERTY, CORPORATE, FAMILY, CONSUMER, ETHICS, BANKRUPTCY; }
ORM Objects:
JPA Mappings of the tables mentioned earlier with the "Entity" suffix added to their table names. They are omitted for brevity. Each one contains getters and setters like JPA Pojos do. If someone asks for the Persistence objects code I will post it.
The only time my domain objects are aware of the save model is in my factory's CodificationChartFactory object, which uses the repository interfaces that I use to interact with the ORM objects mentioned earlier. This factory is the only part of the domain that uses persistence repositories, thus the only part that interacts with the persistence level.
Does a separate domain model create wasteful efforts here? I can see how this is possible in order to put my CodificationChart behavior into a Persistence object. Itβs just somehow not the case to put these behaviors into a persistence object, which is only tasked to retrieve the record from the database.
I will definitely be fixed.