Everyone should have heard of the name of DDD domain driver design, but few of the actual projects are completely implemented in DDD. Because DDD concepts are complex, such as domain, sub domain, core sub domain, general sub domain, entity, value object, domain service, application service, domain event, context and so on, people are directly confused. When corresponding to the actual business model, it is difficult for developers to reach an agreement.
Because DDD was originally designed as a solution for complex software, but most of our applications are not so complex. It’s a bit self defeating for a simple application to use such a complex set of concepts. In the era of microservice, the design principle is to divide the context according to the domain, which greatly reduces the complexity of single application. Microservice needs a simple architecture.
Here I propose a lightweight DDD architectureDDD LiteTo make it better fit the micro service.
Hierarchical architecture is widely used in MVC, and DDD also has four tier architecture, five tier architecture, hexagon architecture and other genres. DDD Lite adopts a relatively simple four tier architecture, from top to bottom as follows:
HTTP, RPC and other interfaces are provided for external users. The logic of parameter verification, encoding and decoding are processed in this layer, and the business status code and external data structure are defined in this layer.
Main business logic layer, calling model layer repository interface to realize domain business logic and transaction assembly. A service corresponds to a domain, which refers to the classification of similar business logic. Generally, a micro service has only one or two services, which should not be too many.
Data model layer.
Define the data structure and the repository interface corresponding to the data model, but do not include the specific implementation. Multiple related models constitute the domain. The domain is simplified here, and no longer entangled in the fine-grained concepts such as sub domain, entity and value object. The repository interface is defined around the model, which is free of business logic and can be called by the service layer to facilitate the composition of transactions in the service layer.
Let’s talk about the repository design pattern separately, which is the top priority of designing a good data model.
Repository mode abstracts the operation of data structure into interface, separates business logic from data access, and adds an abstraction layer.
With the repository interface, dependency becomes abstract from concrete, decouples from DB and other basic layer dependencies, and facilitates the implementation of TDD (Test Driven Development).
Repository interface should be fine-grained, universal enough to reduce business attributes and facilitate reuse.
Realize the model layer repository interface, dock dB, MQ and other data persistence, or RPC remote call back-end services.
At the same time, it can also provide a repository fake implementation for service layer unit testing.
There are many methodologies in DDD, most of which have been seen or practiced in project engineering. But the preconditions required by DDD are too ideal, such as what field experts, common language and so on. In the environment of migrant workers’ agile development, constantly changing demands, and 996 acceleration, the preconditions are too illusory to be implemented. Therefore, we do not need to copy everything. We should adjust measures to local conditions, extract the essence and discard the essence, and extract the suitable application framework. DDD Lite simplifies the DDD design mode, which is the author’s summary and Reflection on the theory of DDD combined with practical engineering experience.
DDD Lite architecture is only for personal understanding. If you have any opinions, please exchange them.
Full project code:win5do/go-microservice-demo (github.com)
DDD hierarchical architecture:https://www.yyang.io/2015/12/31/DDD-and-Layered-Architecture/
DDD Lite architecture proposed by foreign leaders:https://threedots.tech/post/ddd-lite-in-go-introduction/
Domestic tycoons fully follow the go demo designed by DDD, which is slightly cumbersomehttps://github.com/agiledragon/ddd-sample-in-golang