Clean Architecture

Clean architecture puts the business logic and application model at the center of the application. Instead of having business logic depend on data access or other infrastructure concerns, this dependency is inverted: infrastructure and implementation details depend on the Application Core.?

Dependencies flow toward the innermost circle. The Application Core takes its name from its position at the core of this diagram. And you can see on the diagram that the Application Core has no dependencies on other application layers. The application's entities and interfaces are at the very center. Just outside, but still in the Application Core, are domain services, which typically implement interfaces defined in the inner circle. Outside of the Application Core, both the UI and the Infrastructure layers depend on the Application Core, but not on one another (necessarily)

Note that the solid arrows represent compile-time dependencies, while the dashed arrow represents a runtime-only dependency. With the clean architecture, the UI layer works with interfaces defined in the Application Core at compile time, and ideally shouldn't know about the implementation types defined in the Infrastructure layer. At run time, however, these implementation types are required for the app to execute, so they need to be present and wired up to the Application Core interfaces via dependency injection.

Because the Application Core doesn't depend on Infrastructure, it's very easy to write automated unit tests for this layer. Since the UI layer doesn't have any direct dependency on types defined in the Infrastructure project, it's likewise very easy to swap out implementations, either to facilitate testing or in response to changing application requirements. ASP.NET Core's built-in use of and support for dependency injection makes this architecture the most appropriate way to structure non-trivial monolithic applications.

Organizing code in Clean Architecture

In a Clean Architecture solution, each project has clear responsibilities. As such, certain types belong in each project and you'll frequently find folders corresponding to these types in the appropriate project.

Application Core

The Application Core holds the business model, which includes entities, services, and interfaces. These interfaces include abstractions for operations that will be performed using Infrastructure, such as data access, file system access, network calls, etc. Sometimes services or interfaces defined at this layer will need to work with non-entity types that have no dependencies on UI or Infrastructure. These can be defined as simple Data Transfer Objects (DTOs).

Application Core types

·???????Entities (business model classes that are persisted)

·???????Aggregates (groups of entities)

·???????Interfaces

·???????Domain Services

·???????Specifications

·???????Custom Exceptions and Guard Clauses

·???????Domain Events and Handlers

?

Infrastructure

The Infrastructure project typically includes data access implementations. In a typical ASP.NET Core web application, these implementations include the Entity Framework (EF) DbContext, any EF Core?Migration?objects that have been defined, and data access implementation classes. The most common way to abstract data access implementation code is through the use of the?Repository design pattern.

In addition to data access implementations, the Infrastructure project should contain implementations of services that must interact with infrastructure concerns. These services should implement interfaces defined in the Application Core, and so Infrastructure should have a reference to the Application Core project.

Infrastructure types

·???????EF Core types (DbContext,?Migration)

·???????Data access implementation types (Repositories)

·???????Infrastructure-specific services (for example,?FileLogger?or?SmtpNotifier)

?

UI Layer

The user interface layer in an ASP.NET Core MVC application is the entry point for the application. This project should reference the Application Core project, and its types should interact with infrastructure strictly through interfaces defined in Application Core. No direct instantiation of or static calls to the Infrastructure layer types should be allowed in the UI layer.

UI Layer types

·???????Controllers

·???????Custom Filters

·???????Custom Middleware

·???????Views

·???????ViewModels

·???????Startup

The?Startup?class or?Program.cs?file is responsible for configuring the application, and for wiring up implementation types to interfaces. The place where this logic is performed is known as the app's?composition root, and is what allows dependency injection to work properly at run time.


要查看或添加评论,请登录

Murugan K的更多文章

  • .NET Aspire

    .NET Aspire

    .NET Aspire is a cloud-native development stack for building distributed applications in .

    1 条评论
  • Deployment Slots

    Deployment Slots

    Azure Deployment Slots are a feature of Azure App Service (used for hosting web apps, mobile app backends, and APIs)…

  • Microservice - Circuit Breaker Pattern

    Microservice - Circuit Breaker Pattern

    A circuit breaker is a design pattern used in software development to improve the resilience and fault tolerance of…

    1 条评论
  • Docker

    Docker

    Docker is a containerization platform which packages your application and all its dependencies together in the form of…

    1 条评论
  • RabbitMQ

    RabbitMQ

    RabbitMQ serves as a message broker, responsible for sending and receiving messages through queues. RabbitMQ…

    3 条评论
  • Strategy Design Pattern

    Strategy Design Pattern

    Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary…

    2 条评论
  • Playwright - Test (React)

    Playwright - Test (React)

    Playwright is an open-source test automation library initially developed by Microsoft contributors. It supports…

  • Deployment Groups

    Deployment Groups

    Deployment Groups In simple terms, a deployment group is a collection of machines (e.g.

  • Advanced Web Application Architecture

    Advanced Web Application Architecture

    This base application architecture helps to define the latest pattern.

    1 条评论
  • Azure Notification Hub

    Azure Notification Hub

    Azure Notification Hubs provide an easy-to-use and scaled-out push engine that enables you to send notifications to any…

社区洞察

其他会员也浏览了