You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+17-18Lines changed: 17 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,54 +1,53 @@
1
1
# Database-centric vs domain-centric architecture in ASP.NET Core 3.0
2
-
In this sample project we'll review what is the difference between database-centric and domain-centric approach.
2
+
In this sample project we'll review what is the difference between database-centric and domain-centric approaches.
3
3
4
4
The project is built using ASP.NET Core 3.0 and .NET Standard 2.1.
5
5
6
6
7
7
### What we will focus on:
8
8
- Difference between database-centric and domain-centric architecture
9
9
- Why we should consider domain-centric approach
10
-
- How to rebuild an application from database to domain-centric
10
+
- How to rebuild an application from database-centric to domain-centric
11
11
12
12
13
13
### What we will not look at:
14
-
- Architectural styles such as Hexagonal Architecture, The Onion Architecture, The Clean Architecture
15
14
- Domain-Driven-Design practices
16
15
17
16
18
17
## Database-centric approach
19
-
Let's first understand what is database-centric approach. If we review a traditional 3-tier architecture what we have most commonly is presentation, business and data access layers. Presentation layer takes care for the input/output, business layer is where we have our domain specific rules and the data access layer is where we persist the data.
18
+
Let's first understand what is database-centric approach. If we review a traditional 3-tier architecture what we have most commonly is presentation, business and data access layers. Presentation layer takes care for the input/output, business layer is where we define our domain specific rules and the data access layer is provides simplified access to the database.
20
19
21
20

22
21
23
-
The dependencies in that architecture goes in a way:
22
+
The dependencies goes in a way:
24
23
- Presentation layer depends on business layer
25
-
- Business layer depends on persistence layer and any external dependencies
24
+
- Business layer depends on persistence layer and any external dependencies (message brokers, other APIs, etc.)
26
25
- Persistence layer talks directly to a database
27
26
28
27
29
-
## The problem with database-centric approach
30
-
If we examine the previous diagram in more details we might spot a problem in our business layer. The business or domain logic is the most important part of our solution and because of that we want to make sure it's stable enough. What I mean by that is we must have less dependencies to the outside world, which can force us to make changes in business layer.
28
+
## The problem with the database-centric approach
29
+
If we examine the previous diagram in more details we might spot a problem in our business layer. The business or domain logic is the most important part of our solution and because of that we want to make sure it's stable enough. What I mean by that is we must have less dependencies to the outside world, which can force us to make changes in the business layer.
31
30
32
-
Right now our business layer depends on the persistence layer and external components, which means the domain logic might have visibility on database details. In a way we're now coupling our domain with data persistence details. Any change that we introduce inside persistence layer might cause a change inside business layer as well, same for the external components. The problem with dependencies is that something very concrete as changing the repository method because of some MongoDb specific requirement, will break our business rules, then we must fix them which is always introducing risk. What makes more sense is to change the way we persist the data, because of a modification in business rules, but not the other way around.
31
+
Right now our business layer depends on the persistence layer and external agencies, which means the domain logic might have visibility on database details. In a way we're now coupling our domain with data persistence details, which makes it impossible to replace the database without breaking our domain. Any change that we introduce inside persistence layer might cause a change inside business layer as well, same for the external components. The problem with dependencies is that something very concrete as changing the repository method because of some MongoDb specific requirement, will break our business rules, then we must fix them which is always introducing risk. What makes more sense is to change the way we persist the data, because of a modification in business rules, but not the other way around.
33
32
34
33
The end goal is to be able to migrate from SQL Server to MySQL without changing anything inside business layer.
35
-
In real case scenario we might retrieve data from 3rd party application through HTTP protocol or use our own database, the business layer must not be concerned with such details.
34
+
In real case scenario we might retrieve data from 3rd party application through HTTP protocol or use our own database, the domain must not be concerned with such details.
36
35
37
36
So what's the solution?
38
37
39
38
40
39
## Domain-centric approach
41
-
Domain-centric architecture describes an approach where the heart of our software is the business (application) layer and the domain. In order to have such architecture, we must make our business layer independent from details as the database, it shouldn't be concerned with the question how we persist the data. In comparison with the database-centric approach now all the dependencies are pointing towards our domain.
40
+
Domain-centric architecture describes an approach where the heart of our software is the business (application) layer and the domain. In order to have such architecture, we must make our business layer independent from details as the database, it shouldn't be concerned with the question where we persist the data. In comparison with the database-centric approach now all the dependencies are pointing towards our domain.
42
41
43
42
In domain-centric application a change in a database will not affect our business rules, because they never depended on the database. A way to achieve that is by applying the [inversion of control principle](https://en.wikipedia.org/wiki/Inversion_of_control).
44
43
45
-
Domain-centric approach gives us the freedom to easy change between databases and use the right tool for the right job without coupling to it. For different types of data we might want to use databases such as Redis, Amazon Aurora, MongoDb, CosmosDb, etc. and using any of them would not break our domain.
44
+
Domain-centric approach gives us the freedom to easy change databases and use the right tool for the right job without coupling to it. For different types of data we might want to use databases such as Redis, Amazon Aurora, MongoDb, CosmosDb, etc. and using any of them would not break our domain.
46
45
47
46

48
47
49
48
50
49
## Architectural styles
51
-
The idea of domain-centric approach was born years back and nowadays there are different architectural styles which are sharing the same principles. Few of them are:
50
+
Nowadays there are different architectural styles which are sharing the same principles from the domain-centric approach. Few of them are:
52
51
- Hexagonal Architecture (a.k.a. Ports and Adapters)
@@ -60,7 +59,7 @@ In all of them we'll see that the main goal is keeping our business rules indepe
60
59
61
60
62
61
### Use case
63
-
Let's consider the following use case. We have a car dealership inventory system. One of the basic operations that we support is adding a new car. This operation might have some rules such as make and model are required, the car should be built after 1990, etc.
62
+
Let's consider the following use case. We have a car dealership inventory system. One of the basic operations that we support is adding a new car. This operation might have some rules such as make and model are required, the car should be built after 1990.
64
63
65
64
For IO channel we'll use ASP.NET Core 3.0 application which is responsible for handling HTTP requests. After a request hits the API, we'll pass the information to our business layer and then we will store the information inside in-memory database.
66
65
@@ -85,7 +84,7 @@ Another way to solve the same problem, but in domain-centric way is to inverse d
85
84
86
85

87
86
88
-
In this example we still keep api, application and persistence layers, but this time our service doesn't know for any data access related details. Now we have extracted `ICarRepository` inside the application layer and the implementation is done inside the persistence, after that only application layer can dictate changes. If we decide to replace SQL Server database with MySQL, we must implement it in a way that it fits the contract of the application layer.
87
+
In this example we still keep the API, application and persistence layers, but this time our service doesn't know for any data access related details. Now we have extracted `ICarRepository` inside the application layer and the implementation is done inside the persistence, after that only application layer can dictate changes. If we decide to replace SQL Server database with MySQL, we must implement it in a way that it fits the contract of the application layer.
89
88
90
89

91
90
@@ -95,7 +94,7 @@ Comparison of code maps:
95
94
96
95
## Final words
97
96
98
-
Domain-centric approach provides:
99
-
- better testability
97
+
Domain-centric approach results in:
98
+
- better testability of our domain
100
99
- loosely coupled solution
101
-
- independence of database
100
+
- independence of external agencies (database, web, etc.)
0 commit comments