Handling Complex Business Scenarios with Domain Modeling — Part 2

We spoke with Alibaba senior technical expert Zhang Jianfei about domain modeling and its benefits and applications. In Part 1 of the article, we talked about to talk about why domain modeling is necessary and its benefits. In this section, we will discuss what a good model is like by looking at a real implementation of domain modeling for bank transfers.

Domain Services

What Is a Domain Service?

Actions in some domains are multiple verbs but don’t look like they belong to an object. They represent an important behavior in a domain, so we can neither ignore them nor merge them into an entity or value object. If you recognize this behavior in the domain, the best practice is to declare it as a service. Such an object no longer has a built-in state. Its role is only to provide corresponding functions for the domain. We often name a service after an activity rather than an entity. For example, in the case of an initial transfer, the transfer is a critical domain concept, but it is between two accounts. Attributing it to the account entity doesn’t make sense. In this kind of situation, it’s more appropriate to use the MoneyTransferDomainService.

  1. The operation involves another object in the domain.
  2. Operations are stateless.

How Do We Separate Application Services and Domain Services?

In domain modeling, we generally divide the system into three primary layers: the application layer, domain layer, and infrastructure layer. For details on these three layers, refer to the layer design of my other article on SOFA. We can see that both the App layer and Domain layer have services. How do we separate these services? What kinds of functions should we place on the application layer? What kinds of services should we place on the domain layer?

Event Script Implementation of Bank Transfers

In the implementation of the event script, the domain business logic for transferring money between two accounts is written into the implementation of MoneyTransferService, while Account is just the data structure of getters and setters. We call this the anemia model.

Implementation of Domain Model for Bank Transfers

If we use an implementation of DDD, Account not only includes account attributes but behavior and business logic, such as the debit() and credit() methods.

Business Visualization and Configurability

Good domain modeling can reduce the complexity of the application. Also, visualization and configurability primarily help everyone (mainly non-technical people like the product, business, and client management personnel) intuitively understand the system and configure it. Providing a “code free” solution is the main selling point of SaaS software. One aspect we should pay attention to is that visualization and configurability unavoidably add complexity to the system. We have to tread with caution upon caution. It is best to couple the visualization and configuration logic with the business logic as little as possible. Otherwise, the original architecture can get destroyed; so making things more complicated is far from worthwhile.

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com