DDD (domain driven design)

wahyu eko hadi saputro
4 min readMay 1, 2020

--

Hi all, at this corona pandemic time I will write article about DDD (domain driven design). I have already read some pages of book “Domain-Driven Design: Tackling Complexity in the Heart of Software” by eric evan. It is old book but the content is still relevant for today. I write this article to improve my understanding about the content of this book. A paragraph of the book talks about domain “Every software program relates to some activity or interest of its user. That subject area to which the user applies the program is the domain of the software. Some domains involve the physical world: The domain of an airline-booking program involves real people getting on real aircraft. Some domains are intangible: The domain of an accounting program is money and finance. Software domains usually have little to do with computers, though there are exceptions: The domain of a source-code control system is software development itself”. Domain is place, you can call your home is your domain. Domain is your territory , your scope of work for example domain of teacher is teaching, and domain of students are learning.

DDD is how to transform a business process to a software, all terms or languages in the business process must be exist in the software, thus all business users and software engineers have same understanding. In transformation process you can implement some architectures like onion architecture or N-layered architecture.

DDD has some building block to construct an application :

a. Ubiquitous Language : Ubiquitous Language is where all domain experts / business users, architects , engineers / developers use same term and languages. Domain experts are everyone who in charge in the business process (business users). Architects are everyone who create software architecture based on business process. engineers / developers are everyone who develop software. Example in mobile banking system:
Case of business user: Inhouse Transfer : inhouse transfer is transfer amount from one account to another acoount in same bank.
Architects creates architecture :
- Mobile banking system will have InhouseTransfer menu
- The properties of InhouseTransfer menu are : accountFrom, accountTo, amount, these properties will be sent to banking core system.
Developers coding implementation :
- creating file InhouseTransferController as application service
- creating file InhouseTransferService as domain service
- creating file InhouseTransferEntity as Entity. The properties of InhouseTransferEntity are id as unique identity, accountFrom, accountTo, amount.
- creating file InhouseTransferRepository as Repository

b. Entity : entity is a object that has one unique properties. Comparing to RDMS entity is master data table, master data table always has primary key as unique identity. From the existing business process we should identify where is entity and where is not entity. For example in mobile banking system we can create entity with name Account that contain properties: accountNo and accountName. accountNo is unique property.

public class Account {private String accountNo; //uniqueprivate String accountName;public String getAccountNo() {return accountNo;}public void setAccountNo(String accountNo) {this.accountNo = accountNo;}public String getAccountName() {return accountName;}public void setAccountName(String accountName) {this.accountName = accountName;}}

c. Value Object : Value Object must be immutable. In java programming language we can use public static final String name = “sven”, or enum class :

public static enum Error{ERROR_DATA_500("ERR-00000", "Oops something when wrong"),ERROR_DATA_ALREADY_EXIST("ERR-00001", "Data Already Exist"),ERROR_DATA_NOT_FOUND("ERR-00002", "Data Not Found"),ERROR_HTTP_TYPE_NOT_YET_REGISTERED("ERR-00003", "HTTP type is not registered");private String code;private String errorMessage;private Error(String code, String errorMessage) {this.code = code;this.errorMessage = errorMessage;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getErrorMessage() {return errorMessage;}public void setErrorMessage(String errorMessage) {this.errorMessage = errorMessage;}}

d. Repository or DAO (Data access object) : the most of java repository implements concept of in memory persistence. The popular framework of in memory persistence are java hibernate, spring JPA (java persistence api), etc.

e. Factories : Factories are responsible in creating object. In book head first design pattern there is factory pattern, the factory pattern will create the needed object.

Factory java class

f. Domain service : Domain service contains logic and stateless. In java spring framework, it uses service annotation (@Service) to represent that the java file is for logical purpose. Stateless means no data collision for each request. In java programming we can achieve stateless by coding in method.

g. Application service : application service is stateless thin layer and in java spring framework, it uses controller/RestController annotation (@RestController) to represent outer layer of backend. Stateless means no data collision for each request. In java programming we can achieve stateless by coding in method.

h. Layering : layering is used to separate of concern.

But For layering architecture my preferation is Onion Architecture layer, you can find information about onion on https://jeffreypalermo.com/2008/07/the-onion-architecture-part-1/

--

--

No responses yet