- Mastering Microservices with Java 9(Second Edition)
- Sourabh Sharma
- 488字
- 2021-07-02 21:54:50
Services
While creating the domain model, you may encounter various situations where behavior may not be related to any object specifically. These behaviors can be accommodated in service objects.
Service objects are part of domain layer that does not have any internal state. The sole purpose of service objects is to provide behavior to the domain that does not belong to a single entity or value object.
Ubiquitous language helps you to identify different objects, identities, or value objects with different attributes and behaviors during the process of domain modeling. During the course of creating the domain model, you may find different behaviors or methods that do not belong to any specific object. Such behaviors are important, and so cannot be neglected. Neither can you add them to entities or value objects. It would spoil the object to add behavior that does not belong to it. Keep in mind, that behavior may impact on various objects. The use of object-oriented programming makes it possible to attach to some objects; this is known as a service.
Services are common in technical frameworks. These are also used in domain layers in DDD. A service object does not have any internal state; the only purpose of it is to provide a behavior to the domain. Service objects provide behaviors that cannot be related to specific entities or value objects. Service objects may provide one or more related behaviors to one or more entities or value objects. It is a practice to define the services explicitly in the domain model.
While creating the services, you need to tick all of the following points:
- Service objects' behavior performs on entities and value objects, but it does not belong to entities or value objects
- Service objects' behavior state is not maintained, and hence, they are stateless
- Services are part of the domain model
Services may also exist in other layers. It is very important to keep domain-layer services isolated. It removes the complexities and keeps the design decoupled.
Let's take an example where a restaurant owner wants to see the report of his monthly table bookings. In this case, he will log in as an admin and click the Display Report button after providing the required input fields, such as duration.
Application layers pass the request to the domain layer that owns the report and templates objects, with some parameters such as report ID, and so on. Reports get created using the template, and data is fetched from either the database or other sources. Then the application layer passes through all the parameters, including the report ID to the business layer. Here, a template needs to be fetched from the database or another source to generate the report based on the ID. This operation does not belong to either the report object or the template object. Therefore, a service object is used that performs this operation to retrieve the required template from the database.
- Instant Node Package Manager
- Magento 2 Theme Design(Second Edition)
- Reactive Programming With Java 9
- 人人都是網(wǎng)站分析師:從分析師的視角理解網(wǎng)站和解讀數(shù)據(jù)
- Visual C++數(shù)字圖像處理技術(shù)詳解
- Mastering Ext JS
- 青少年P(guān)ython編程入門
- R語(yǔ)言與網(wǎng)絡(luò)輿情處理
- Python圖形化編程(微課版)
- Extreme C
- Machine Learning for Developers
- Drupal 8 Development:Beginner's Guide(Second Edition)
- 面向?qū)ο蟪绦蛟O(shè)計(jì)及C++(第3版)
- 深入理解Java虛擬機(jī):JVM高級(jí)特性與最佳實(shí)踐
- Unity 5 Game Optimization