.net core 整潔架構入門

Clean Architecture with .NET Core: Getting Started

# 使用.NET Core整潔架構(Clean Architecture):入門
Over the past two years, I’ve travelled the world teaching programmers how to build enterprise applications using Clean Architecture with .NET Core. I started by providing a sample solution using the iconic Northwind Traders database. Recently, I’ve developed a new Clean Architecture Solution Template for .NET Core.
在過去的兩年中,我走遍世界,教程序員如何使用帶有.NET Core的整潔架構來構建企業應用程序。我首先使用標誌性的Northwind Traders數據庫提供了示例解決方案。最近,我爲.NET Core開發了一個新的整潔架構解決方案模板。

This post provides an overview of Clean Architecture and introduces the new Clean Architecture Solution Template, a .NET Core Project template for building applications based on Angular, ASP.NET Core 3.1, and Clean Architecture.
這篇文章概述了整潔架構,並介紹了新的整潔架構解決方案模板,這是一個.NET Core項目模板,用於基於Angular,ASP.NET Core 3.1和Clean Architecture構建應用程序。

Let’s start with an overview of Clean Architecture.
讓我們從整潔架構的概述開始。

Overview

概覽

With Clean Architecture, the Domain and Application layers are at the centre of the design. This is known as the Core of the system.
使用整潔架構,領域層和應用程序層是設計的中心。這被稱爲系統的核心。

The Domain layer contains enterprise logic and types and the Application layer contains business logic and types. The difference is that enterprise logic could be shared across many systems, whereas the business logic will typically only be used within this system.
領域層包含企業邏輯和類型,應用程序層包含業務邏輯和類型。區別在於企業邏輯可以在許多系統之間共享,而業務邏輯通常僅在該系統內使用。

Core should not be dependent on data access and other infrastructure concerns so those dependencies are inverted. This is achieved by adding interfaces or abstractions within Core that are implemented by layers outside of Core. For example, if you wanted to implement the Repository pattern you would do so by adding an interface within Core and adding the implementation within Infrastructure.
核心層不應依賴於數據訪問和其他基礎結構層,因此這些依賴關係可以反轉。這是通過在Core中添加由Core外部各層實現的接口或抽象來實現的。例如,如果要實現存儲庫模式,可以通過在Core中添加接口並在基礎射設施層中添加實現來實現。

All dependencies flow inwards and Core has no dependency on any other layer. Infrastructure and Presentation depend on Core, but not on one another.
所有依賴關係都向內流動,而Core不依賴於其他任何層。基礎架構和展示層依賴於Core,而不是彼此依賴。
整潔架構圖
Figure: Clean Architecture Diagram
圖:清潔架構圖

This results in architecture and design that is:
整潔架構的架構和設計如下:

  • Independent of frameworks it does not require the existence of some tool or framework
  • 獨立於框架,它不需要某些工具或框架
  • Testable easy to test – Core has no dependencies on anything external, so writing automated tests is much easier
  • 易於測試 核心層不依賴於外部任何層,因此編寫自動化測試要容易得多
  • Independent of UI logic is kept out of the UI so it is easy to change to another technology – right now you might be using Angular, soon Vue, eventually Blazor!
  • 獨立與UI邏輯,輯不包含在UI中,因此可以輕鬆地更改爲另一種技術–現在,您可能正在使用Angular,以後可能會用Vue或Blazor!
  • Independent of the database data access concerns are cleanly separated so moving from SQL Server to CosmosDB or otherwise is trivial
  • 完全獨立於數據庫,數據訪問方與業務邏輯已完全分開,因此可以輕鬆地從SQL Server遷移到CosmosDB或其他數據庫
  • Independent of anything external
  • in fact, Core is completely isolated from the outside world – the difference between a system that will last 3 years, and one that will last 20 years
  • 不依賴於任何外部事物 實際上,Core不受外界影響,完全與外界隔離-這是一個持續3年的系統與一個持續20年的系統之間的區別

In the above design, there are only three circles, you may need more. Think of this as a starting point. Just remember to keep all dependencies pointing inwards.
在上面的設計中,只有三個圓圈,您可能需要更多。將此視爲起點。只要記住讓所有依賴項指向內部即可。

Let’s take a look at a simple approach to getting started with the new Clean Architecture Solution Template.
讓我們來看一個使用新的Clean Architecture解決方案模板的簡單實現。

Solution template

解決方案模板

This template provides an awesome approach to building solutions based on ASP.NET Core 3.1 and Angular 8 that follow the principles of Clean Architecture. If Angular is not your thing, worry not, you can remove it with ease. In this section, you will install the template, create a new solution, and review the generated code.
該模板爲基於ASP.NET Core 3.1和Angular 8,並遵循整潔架構原則的解決方案提供了一種很棒的方法。如果你不用Angular,請不用擔心,可以輕鬆將其替換掉。在本節中,您將安裝模板,創建新的解決方案,並查看生成的代碼。

Prerequisites

準備工作

The first step is to ensure you meet the following prerequisites:
第一步是確保您滿足以下先決條件:

  • .NET Core SDK (3.1 or later)
  • Node.js (6 or later)
    Check the .NET Core version by running this command:
    通過運行以下命令檢查.NET Core版本:
    dotnet --list-sdks
    Check the node version by running this command:
    通過運行以下命令檢查節點版本:
    node -v
    Next, install the solution template using this command:
    接下來,使用以下命令安裝解決方案模板:
    dotnet new --install Clean.Architecture.Solution.Template

Create a new solution

創建一個新的解決方案

Creating a new solution is easy. Within an empty folder,run the following command:
創建新的解決方案很容易。在一個空文件夾中,運行以下命令:
dotnet new ca-sln
The following message will be displayed:
將顯示以下消息:
The template "Clean Architecture Solution" was created successfully.

This command will create a new solution, automatically namespaced using the name of the parent folder. For example, if the parent folder is named Northwind, then the solution will be named Northwind.sln, and the default namespace will be Northwind.
該命令將創建一個新的解決方案,並使用父文件夾的名稱自動命名。例如,如果父文件夾名爲Northwind,則解決方案將命名爲Northwind.sln,默認命名空間爲Northwind。

The solution is built using the Angular project template with ASP.NET Core. The ASP.NET Core project provides an API back end and the Angular CLI project provides the UI.
該解決方案是使用帶有ASP.NET Core的Angular項目模板構建的。 ASP.NET Core項目提供API後端,而Angular CLI項目提供UI。

Note
Read Use the Angular project template with ASP.NET Core to learn more about this approach.

Launching the solution from Visual Studio 2019 is trivial, just press F5.
從Visual Studio 2019啓動解決方案很簡單,只需按F5鍵即可。

In order to launch the solution using the .NET Core CLI, a few more steps are required. You can learn more by visiting the above link, but I’ll include the information here for completeness.
爲了使用.NET Core CLI啓動解決方案,還需要執行幾個步驟。您可以通過上面的鏈接瞭解更多信息,但是爲了完整起見,我將在此處提供這些信息。

First, you will need an environment variable named ASPNETCORE_Environment with a value of Development. On Windows, run SET ASPNETCORE_Environment=Development. On Linux or macOS, run export ASPNETCORE_Environment=Development.
首先,您將需要一個名爲ASPNETCORE_Environment的環境變量,其值爲Development。在Windows上,運行SET ASPNETCORE_Environment =開發。在Linux或macOS上,運行export ASPNETCORE_Environment = Development。

Next, run the following command from the solution folder:
接下來,從解決方案文件夾運行以下命令:

cd src/WebUI
dotnet build

Note
The initial build will take a few minutes, as it will also install required client-side packages. Subsequent builds will be much quicker.

注意
初始構建將花費幾分鐘,因爲它還將安裝所需的客戶端軟件包。隨後的構建將更快。

Then run dotnet run to start the application. The following message will be displayed:
然後運行dotnet run啓動應用程序。將顯示以下消息:
Now listening on: https://localhost:port

The port is usually 5001. Open the web site by navigating to https://localhost:port.
端口通常是5001。通過導航到https:// localhost:port來打開網站。

Note
You will also see a message similar to the following:
NG Live Development Server is listening on localhost:port, open your browser on http://localhost:port
Ignore this message, it’s not the URL for the combined

注意
ASP.NET Core and Angular CLI application
忽略此消息,它不是組合的ASP.NET Core和Angular CLI應用程序的URL

Then run dotnet run to start the application. The following message will be displayed:
然後運行dotnet run啓動應用程序。將顯示以下消息:

Now listening on: https://localhost:port

The port is usually 5001. Open the web site by navigating to https://localhost:port.
端口通常是5001。通過導航到https:// localhost:port來打開網站。

Note
You will also see a message similar to the following: NG Live Development Server is listening on localhost:port, open your browser on http://localhost:port Ignore this message, it’s not the URL for the combined ASP.NET Core and Angular CLI application

注意
您還將看到類似於以下消息:NG Live Development Server正在localhost:port上偵聽,在http:// localhost:port上打開瀏覽器忽略此消息,它不是組合的ASP.NET Core的URL和Angular CLI應用程序

If everything was successful you will see the following:
如果一切成功,您將看到以下內容:
在這裏插入圖片描述
Let’s take a look at the structure of the newly generated solution.
讓我們看一下新生成的解決方案的結構。

Solution structure

解決方案結構

The solution template generates a multi-project solution. For a solution named Northwind, the following folder structure is created:
解決方案模板生成一個多項目解決方案。對於名爲Northwind的解決方案,將創建以下文件夾結構:

[圖片]

The project names within src align closely to the layers of the Clean Architecture diagram, the only exception being WebUI, representing the Presentation layer.
src中的項目名稱與Clean Architecture圖的各層緊密對齊,唯一的例外是WebUI,它表示Presentation層。

The Domain project represents the Domain layer and contains enterprise or domain logic and includes entities, enums, exceptions, interfaces, types and logic specific to the domain layer. This layer has no dependencies on anything external.
領域項目代表領域層,包含企業或領域邏輯,並且包含特定於領域層的實體,枚舉,異常,接口,類型和邏輯。該層不依賴外部任何東西。

The Application project represents the Application layer and contains all business logic. This project implements CQRS (Command Query Responsibility Segregation), with each business use case represented by a single command or query. This layer is dependent on the Domain layer but has no dependencies on any other layer or project. This layer defines interfaces that are implemented by outside layers. For example, if the application needs to access a notification service, a new interface would be added to the Application and the implementation would be created within Infrastructure.
Application項目代表Application層,幷包含所有業務邏輯。該項目實現了CQRS(命令查詢責任隔離),每個業務用例均由單個命令或查詢表示。該層依賴於“領域”層,但不依賴於任何其他層或項目。該層定義了由外部層實現的接口。例如,如果應用程序需要訪問通知服務,則將新接口添加到應用程序,並在基礎架構中創建實現。

The Infrastructure project represents the Infrastructure layer and contains classes for accessing external resources such as file systems, web services, SMTP, and so on. These classes should be based on interfaces defined within the Application layer.
基礎結構項目代表基礎結構層,幷包含用於訪問外部資源(例如文件系統,Web服務,SMTP等)的類。這些類應基於在應用程序層內定義的接口。

The WebUI project represents the Presentation layer. This project is a SPA (single page app) based on Angular 8 and ASP.NET Core. This layer depends on both the Application and Infrastructure layers. Please note the dependency on Infrastructure is only to support dependency injection. Therefore Startup.cs should include the only reference to Infrastructure.
WebUI項目表示展示層。該項目是基於Angular 8和ASP.NET Core的SPA(單頁應用程序)。該層取決於應用程序層和基礎結構層。請注意,對基礎結構的依賴關係僅支持依賴關係注入。因此,Startup.cs應該包括對基礎結構的唯一引用。

Tests

測試

The tests folder contains numerous unit and integration tests projects to help get you up and running quickly. The details of these projects will be explored in a follow-up post. In the meantime, feel free to explore and ask any questions below.
tests文件夾包含許多單元和集成測試項目,以幫助您快速啓動和運行。這些項目的詳細信息將在後續文章中進行探討。同時,請隨時探索和詢問以下任何問題。

Technologies

Aside from .NET Core, numerous technologies are used within this solution including:
除了.NET Core,此解決方案中還使用了許多技術,包括:

  • CQRS with MediatR
  • 具有MediatR的CQRS
  • Validation with FluentValidation
  • FluentValidation驗證
  • Object-Object Mapping with AutoMapper
  • 使用AutoMapper進行對象-對象映射
  • Data access with Entity Framework Core
  • 使用Entity Framework Core進行數據訪問
  • Web API using ASP.NET Core
  • 使用ASP.NET Core的Web API
  • UI using Angular 8
  • 使用Angular 8的UI
  • Open API with NSwag
  • 使用NSwag打開API
  • Security using ASP.NET Core Identity + IdentityServer
  • 使用ASP.NET Core Identity + IdentityServer的安全性
  • Automated testing with xUnit.net, Moq, and Shouldly
  • 使用xUnit.net,Moq和Shouldly進行自動化測試

In follow-up posts, I’ll include additional details on how the above technologies are used within the solution.
在後續文章中,我將提供有關在解決方案中如何使用上述技術的更多詳細信息。

Additional resources

其他資源

In this post, I have provided an overview of Clean Architecture and the new solution template. If you would like to learn more about any of these topics, take a look at the following resources:
在這篇文章中,我提供了Clean Architecture和新解決方案模板的概述。如果您想了解有關這些主題的更多信息,請查看以下資源:

Thanks for reading. Please post any questions or comments below.
謝謝閱讀。請在下面發佈任何問題或評論。

發佈了153 篇原創文章 · 獲贊 44 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章