What is the difference between the responsibilities of the domain layer and the application layer in the onion architecture [DDD]

Series Article: Domain Driven Design Explanation Series

What is the most accessible architecture to get started with Domain Driven Design-little hands' lab
Domain Driven + Onion Architecture Overview --little hands' lab
As I wrote in these articles, I'm DDD We use the term onion architecture when deciding on a layered architecture for. I've explained it to various people so far, and I've talked about layer responsibilities as a point that makes it easy to get stuck in understanding, so I will explain that.

Definition that is difficult to convey in one shot (I think)

Onion Architecture Is Interesting - DZone Java

Description of each layer written in this article

Japanese translation

I've explained it to various people so far, but in many cases I couldn't get it right with the above explanation. Especially when I say *** application-specific logic ***, the word "application" is ambiguous, so people receive it differently, and it doesn't seem to fit well.

Therefore, recently, I have focused on the topic of responsibility and tried to explain as follows.

Responsibility-focused definition

It may be a little misleading or counter-arguing to say that the responsibility of the domain layer is "data integrity", but I dare to say this with an emphasis on ease of communication.

I will explain in detail below.

Domain layer

What does it mean to "ensure data integrity" at this layer?

For a concrete code example, please refer to the article What is expressing domain knowledge in a model. However, we aim to "only expose methods that can ensure consistency to the upper layer (do not make visibility public).

In the example of the above article, the rule of data integrity is that "a task can be postponed only three times, one day at a time." In order to guarantee this, it is necessary to perform controls such as "record the number of postponements when postponing a task" and "check the number of postponements so far before postponing and make an error if it exceeds 3 times".

A method that does not guarantee this is something like "You can freely change the due date without considering the number of task postponements at all". This is exactly what it is like to publish all setters with public settings, like ActiveRecord pattern objects.

By making only the methods that can be controlled correctly public, we aim for a state where "no matter how hard the upper layer works, the data integrity cannot be broken".

Application layer (use case layer)

I wrote that the responsibility of this layer is to "combine the methods exposed by the domain layer and build a use case".

How is this different from the responsibilities of the domain layer?

To give another example, let's say the domain layer exposes a "method to change a user's address" and a "method to change a landline number" as independent public methods.

This means that the domain layer "allows the address and landline number to be updated independently for the sake of data integrity."

On the other hand, in the application layer, the following use cases can be realized by combining the above methods.

Whether you want the update operation to be performed independently or at the same time can be freely combined in the application layer according to the use case you want to realize.

Conversely, if the domain layer says, "For data integrity, addresses and landline numbers can only be updated at the same time," the application layer will have to comply.

Use cases (especially in your own web application) can change in a short cycle depending on the policy. But will the integrity of the data change accordingly? The answer would be no. By confining items with different life cycles in each layer, the design can be made resistant to change.

merit

Finally, I would like to write about the merits of why such layering is done.

Summary

When designing a DDD, it is always necessary to think about "where is this responsibility?" That is a super important concept that is central to DDD's layering design. Please use it as a reference when designing.

Recommended Posts

What is the difference between the responsibilities of the domain layer and the application layer in the onion architecture [DDD]
What is the representation of domain knowledge in the [DDD] model?
What is the difference between a web server and an application server?
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
What is the difference between a class and a struct? ?? ??
What is the difference between System Spec and Feature Spec?
[Rails] What is the difference between redirect and render?
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between skip and pending? [RSpec]
What is the difference between Java EE and Jakarta EE?
[Rails] What is the difference between bundle install and bundle update?
Jersey --What is Difference Between bind and bindAsContract in HK2?
What is the difference between an action and an instance method?
[Java] What is the difference between form, entity and dto? [Bean]
The difference between puts and print in Ruby is not just the presence or absence of line breaks
What is Pullback doing in The Composable Architecture
About the difference between classes and instances in Ruby
Is there a performance difference between Oracle JDK and OpenJDK at the end of 2017?
Understand in 3 minutes! A very rough explanation of the difference between session and cookie
[DDD] What is the most accessible architecture to get started with Domain Driven Design?
[Docker-compose] Difference between env_file and environment. Priority of environment variable application
About the difference between "(double quotation)" and "single quotation" in Ruby
I will explain the difference between Android application development and iOS application development from the perspective of iOS engineers
[Ruby] About the difference between 2 dots and 3 dots of range object.
Now in the third year, the misunderstanding that I noticed is the difference between the equals method and ==
Is short-circuit evaluation really fast? Difference between && and & in Java
Output the difference between each field of two objects in Java
What is @Override or @SuppressWarnings ("SleepWhileInLoop") in front of the function? ?? ??
Difference between byCharWrapping and byWordWrapping of UI Label in Japanese display
[DDD] Domain Driven + Onion Architecture Overview
'% 02d' What is the percentage of% 2?
Understand the difference between each_with_index and each.with_index
Web application structure by Java and processing flow in the presentation layer
The content of the return value of executeBatch is different between 11g and 12c
Understand the difference between int and Integer and BigInteger in java and float and double
The application absorbs the difference in character code
Difference between final and Immutable in Java
What is testing? ・ About the importance of testing
Difference between pop () and peek () in stack
Difference between isEmpty and isBlank of StringUtils
What is the main method in Java?
What is the data structure of ActionText?
Difference between getText () and getAttribute () in Selenium
About the difference between irb and pry
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
Difference between addPanel and presentModally of FloatingPanel
Difference between int and Integer in Java
I tried to make a web application that searches tweets with vue-word cloud and examines the tendency of what is written in the associated profile
[Java] Difference between equals and == in a character string that is a reference type
I received the data of the journey (diary application) in Java and visualized it # 001
[Ruby] What is the slice method? Let's solve the example and understand the difference from slice!
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
Retrieve the first day of week in current locale (what day of the week is it today?)
[Java] Understand the difference between List and Set
[iOS] Understand the difference between frame and bounds
[Rails / ActiveRecord] About the difference between create and create!
Difference between next () and nextLine () in Java Scanner
Understand the difference between abstract classes and interfaces!
Difference between member and collection of rails routes.rb
What are the advantages of DI and Thymeleaf?