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: docs/standard/microservices-architecture/microservice-ddd-cqrs-patterns/infrastructure-persistence-layer-design.md
Copy file name to clipboardExpand all lines: docs/standard/modern-web-apps-azure-architecture/architectural-principles.md
+24-25Lines changed: 24 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,20 +3,18 @@ title: Architectural principles
3
3
description: Architect Modern Web Applications with ASP.NET Core and Azure | Architectural principles
4
4
author: ardalis
5
5
ms.author: wiwagn
6
-
ms.date: 10/06/2017
6
+
ms.date: 6/28/2018
7
7
---
8
-
# Architectural Principles
8
+
# Architectural principles
9
9
10
10
> "If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization."
11
11
> _\- Gerald Weinberg_
12
12
13
-
## Summary
14
-
15
13
You should architect and design software solutions with maintainability in mind. The principles outlined in this section can help guide you toward architectural decisions that will result in clean, maintainable applications. Generally, these principles will guide you toward building applications out of discrete components that are not tightly coupled to other parts of your application, but rather communicate through explicit interfaces or messaging systems.
16
14
17
15
## Common design principles
18
16
19
-
### Separation of Concerns
17
+
### Separation of concerns
20
18
21
19
A guiding principle when developing is **Separation of Concerns**. This principle asserts that software should be separated based on the kinds of work it performs. For instance, consider an application that includes logic for identifying noteworthy items to display to the user, and which formats such items in a particular way to make them more noticeable. The behavior responsible for choosing which items to format should be kept separate from the behavior responsible for formatting the items, since these are separate concerns that are only coincidentally related to one another.
22
20
@@ -28,7 +26,7 @@ Different parts of an application should use **encapsulation** to insulate them
28
26
29
27
In classes, encapsulation is achieved by limiting outside access to the class's internal state. If an outside actor wants to manipulate the state of the object, it should do so through a well-defined function (or property setter), rather than having direct access to the private state of the object. Likewise, application components and applications themselves should expose well-defined interfaces for their collaborators to use, rather than allowing their state to be modified directly. This frees the application's internal design to evolve over time without worrying that doing so will break collaborators, so long as the public contracts are maintained.
30
28
31
-
### Dependency Inversion
29
+
### Dependency inversion
32
30
33
31
The direction of dependency within the application should be in the direction of abstraction, not implementation details. Most applications are written such that compile-time dependency flows in the direction of runtime execution. This produces a direct dependency graph. That is, if module A calls a function in module B, which calls a function in module C, then at compile time A will depend on B which will depend on C, as shown in Figure 4-1.
34
32
@@ -44,23 +42,23 @@ Applying the dependency inversion principle allows A to call methods on an abstr
44
42
45
43
**Dependency inversion** is a key part of building loosely-coupled applications, since implementation details can be written to depend on and implement higher level abstractions, rather than the other way around. The resulting applications are more testable, modular, and maintainable as a result. The practice of *dependency injection* is made possible by following the dependency inversion principle.
46
44
47
-
### Explicit Dependencies
45
+
### Explicit dependencies
48
46
49
47
**Methods and classes should explicitly require any collaborating objects they need in order to function correctly.** Class constructors provide an opportunity for classes to identify the things they need in order to be in a valid state and to function properly. If you define classes that can be constructed and called, but which will only function properly if certain global or infrastructure components are in place, these classes are being *dishonest* with their clients. The constructor contract is telling the client that it only needs the things specified (possibly nothing if the class is just using a default constructor), but then at runtime it turns out the object really did need something else.
50
48
51
49
By following the explicit dependencies principle, your classes and methods are being honest with their clients about what they need in order to function. This makes your code more self-documenting and your coding contracts more user-friendly, since users will come to trust that as long as they provide what's required in the form of method or constructor parameters, the objects they're working with will behave correctly at runtime.
52
50
53
-
### Single Responsibility
51
+
### Single responsibility
54
52
55
53
The single responsibility principle applies to object-oriented design, but can also be considered as an architectural principle similar to separation of concerns. It states that objects should have only one responsibility and that they should have only one reason to change. Specifically, the only situation in which the object should change is if the manner in which it performs its one responsibility must be updated. Following this principle helps to produce more loosely-coupled and modular systems, since many kinds of new behavior can be implemented as new classes, rather than by adding additional responsibility to existing classes. Adding new classes is always safer than changing existing classes, since no code yet depends on the new classes.
56
54
57
55
In a monolithic application, we can apply the single responsibility principle at a high level to the layers in the application. Presentation responsibility should remain in the UI project, while data access responsibility should be kept within an infrastructure project. Business logic should be kept in the application core project, where it can be easily tested and can evolve independently from other responsibilities.
58
56
59
57
When this principle is applied to application architecture, and taken to its logical endpoint, you get microservices. A given microservice should have a single responsibility. If you need to extend the behavior of a system, it's usually better to do it by adding additional microservices, rather than by adding responsibility to an existing one.
60
58
61
-
[Learn more about microservices architecture](http://aka.ms/MicroservicesEbook)
59
+
[Learn more about microservices architecture](https://aka.ms/MicroservicesEbook)
62
60
63
-
### Don't Repeat Yourself (DRY)
61
+
### Don't repeat yourself (DRY)
64
62
65
63
The application should avoid specifying behavior related to a particular concept in multiple places as this is a frequent source of errors. At some point, a change in requirements will require changing this behavior and the likelihood that at least one instance of the behavior will fail to be updated will result in inconsistent behavior of the system.
66
64
@@ -69,47 +67,48 @@ Rather than duplicating logic, encapsulate it in a programming construct. Make t
69
67
> [!NOTE]
70
68
> Avoid binding together behavior that is only coincidentally repetitive. For example, just because two different constants both have the same value, that doesn't mean you should have only one constant, if conceptually they're referring to different things.
71
69
72
-
### Persistence Ignorance
70
+
### Persistence ignorance
73
71
74
72
**Persistence ignorance** (PI) refers to types that need to be persisted, but whose code is unaffected by the choice of persistence technology. Such types in .NET are sometimes referred to as Plain Old CLR Objects (POCOs), because they do not need to inherit from a particular base class or implement a particular interface. Persistence ignorance is valuable because it allows the same business model to be persisted in multiple ways, offering additional flexibility to the application. Persistence choices might change over time, from one database technology to another, or additional forms of persistence might be required in addition to whatever the application started with (for example, using a Redis cache or Azure DocumentDb in addition to a relational database).
75
73
76
74
Some examples of violations of this principle include:
77
75
78
-
-A required base class
76
+
- A required base class.
79
77
80
-
-A required interface implementation
78
+
- A required interface implementation.
81
79
82
-
-Classes responsible for saving themselves (such as the Active Record pattern)
80
+
- Classes responsible for saving themselves (such as the Active Record pattern).
83
81
84
-
-Required default constructor
82
+
- Required default constructor.
85
83
86
-
-Properties requiring virtual keyword
84
+
- Properties requiring virtual keyword.
87
85
88
-
-Persistence-specific required attributes
86
+
- Persistence-specific required attributes.
89
87
90
88
The requirement that classes have any of the above features or behaviors adds coupling between the types to be persisted and the choice of persistence technology, making it more difficult to adopt new data access strategies in the future.
91
89
92
-
### Bounded Contexts
90
+
### Bounded contexts
93
91
94
92
**Bounded contexts** are a central pattern in Domain-Driven Design. They provide a way of tackling complexity in large applications or organizations by breaking it up into separate conceptual modules. Each conceptual module then represents a context which is separated from other contexts (hence, bounded), and can evolve independently. Each bounded context should ideally be free to choose its own names for concepts within it, and should have exclusive access to its own persistence store.
95
93
96
94
At a minimum, individual web applications should strive to be their own bounded context, with their own persistence store for their business model, rather than sharing a database with other applications. Communication between bounded contexts occurs through programmatic interfaces, rather than through a shared database, which allows for business logic and events to take place in response to changes that take place. Bounded contexts map closely to microservices, which also are ideally implemented as their own individual bounded contexts.
0 commit comments