.net core architecture clean entry

Clean Architecture with .NET Core: Getting Started

# Using .NET Core neat architecture (Clean Architecture): Getting Started
. 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.
in the past two years, I have traveled the world to teach programmers how to build enterprise applications using the neat architecture with .NET Core program. I was the first to use the iconic Northwind Traders database provides examples of solutions. I recently developed a new tidy infrastructure solution templates for .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.
This article outlines the clean architecture, and describes new infrastructure solutions neat template, which is a .NET Core project templates for building applications based on Angular, ASP.NET Core 3.1 and clean architecture.

Let's start with an overview of Clean Architecture.
Let's start with an overview of neat architecture.

Overview

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.
Use clean architecture, art, and the application layer is a layer design center. This is called 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.
FIELD layer comprises a business logic and type, application type and contains the business logic layer. The difference is that business logic can be shared between a number of systems, and the business logic normally used only within the 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.
the core layer should not depend on the data access and other infrastructure layer, so these dependencies may be reversed. This is achieved by adding an external interface or abstract a Core layers implemented in the Core. For example, if you want to achieve repository model, may be added to achieve be accomplished by adding an interface and on the basis of the Core layer emitting facility.

All dependencies flow inwards and Core has no dependency on any other layer. Infrastructure and Presentation depend on Core, but not on one another.
All dependencies are flow inwardly and Core does not depend on any other layer. Infrastructure and the presentation layer depends on the Core, rather than relying on one another.
Tidy Chart
Figure: Clean Architecture Diagram
Figure: Cleaning Chart

This results in architecture and design that is :
clean architecture architecture and design as follows:

  • Independent of frameworks it does not require the existence of some tool or framework
  • Independent of the frame , it does not require certain tools or frame
  • Testable easy to test – Core has no dependencies on anything external, so writing automated tests is much easier
  • Easy to test the core layer does not depend on any external layer, and therefore much easier to write automated tests
  • 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!
  • Independence and UI logic , Series not included in the UI, so you can easily change to another technology - now, you may be using Angular, may later use Vue or Blazor!
  • Independent of the database data access concerns are cleanly separated so moving from SQL Server to CosmosDB or otherwise is trivial
  • Completely independent of the database , data access and business logic side has completely separate, so you can easily migrate from SQL Server or other database to 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
  • Not dependent on any external things, in fact, Core free from outside influence, is completely isolated from the outside - it's the difference between a duration of three years of continuous system with a 20-year system

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.
In the above design, only three circles, you may need more. This as a starting point. Just remember that all dependencies can be directed inwardly.

Let's take a look at a simple approach to getting started with the new Clean Architecture Solution Template.
Let's look at a simple realization of the new Clean Architecture solution template.

Solution template

Solution Templates

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.
the template is based on ASP.NET Core 3.1 and Angular 8, and follow the principles tidy infrastructure solution provides a great way. If you do not Angular, do not worry, you can easily replace it. In this section, you will install the template to create new solutions, and look at the generated code.

Prerequisites

Ready to work

The first step is to ensure you meet the following prerequisites:
The first step is to make sure you meet the following prerequisites:

  • .NET Core SDK (3.1 or later)
  • Node.js (6 or later)
    the Check at The .NET Core Version by the Command running the this:
    by running the following command to check the .NET Core version:
    dotnet --list-sdks
    the Check the Node at The Version by the Command running the this:
    by running the following command to check node version:
    node -v
    the Next, install at The solution template using this command:
    Next, use the following command to install solution templates:
    dotnet new --install Clean.Architecture.Solution.Template

Create a new solution

Create a new solution

Creating a new solution is easy Within an empty folder, run the following command:.
Creating new solution is easy. In an empty folder, run the following command:
dotnet new ca-sln
at The following the Message displayed by Will BE:
displays the following message:
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.
The command create a new solution, and use the name of the parent folder automatically named. For example, if the parent folder named Northwind, then the solution will be named Northwind.sln, the default namespace for the 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.
The solution is to use Angular project template with ASP.NET Core of build. ASP.NET Core project provides back-end API, and Angular CLI projects 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.
Start from the Visual Studio 2019 solution is very simple, just press the F5 key.

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.
To start using .NET Core CLI solution also take a few steps. You can learn more about the links above, but for the sake of completeness, I will provide that information here.

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.
First, you will need an environment variable named ASPNETCORE_Environment its value Development. On Windows, run the SET ASPNETCORE_Environment = development. On Linux or macOS, run export ASPNETCORE_Environment = Development.

Next, run the following command from the solution folder:
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.

Note that
the initial build will take a few minutes, because it will be required to install client-side software package. Subsequent building will be faster.

Then run dotnet run to start the application The following message will be displayed:.
Then run dotnet run to start the application. The following message is displayed:
Now listening on: https://localhost:port

Port IS usually 5001. Open at The at The Web Site by the Navigating to HTTPS: // localhost: Port .
Port is typically 5001. By navigating to https: // localhost: port to open the site.

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

Note
ASP.NET Core and Angular CLI application
to ignore this message, URL ASP.NET Core Angular CLI application and it is not combined

Then run dotnet run to start the application The following message will be displayed:.
Then run dotnet run to start the application. The following message is displayed:

Now listening on: https://localhost:port

Port IS usually 5001. Open at The at The Web Site by the Navigating to HTTPS: // localhost: Port .
Port is typically 5001. By navigating to https: // localhost: port to open the site.

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

Note
that you will see something similar to the following message: NG Live Development Server is localhost: listening on port, at http: // localhost: Open your browser to ignore this message on a port, it is not a combination of URL and ASP.NET Core Angular CLI application

If everything was successful you will see the following:
If everything is successful, you will see the following:
Here Insert Picture Description
Let's AT the Take A look at The Structure of newly Generated at The Solution.
Let us look at the structure of the new generation of solutions.

Solution structure

Solution structure

The solution template generates a multi-project solution. For a solution named Northwind, the following folder structure is created:
解决方案模板生成一个多项目解决方案。对于名为Northwind的解决方案,将创建以下文件夹结构:

[image]

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.
Thanks for reading. Please post any questions or comments below.

Guess you like

Origin www.cnblogs.com/AlexanderZhao/p/12310275.html