Clean Architecture
Murugan K
.NET Core| C# | Microservices | SQL | ReactJS | Node.js | CosmosDB | AZURE | AWS | CI/CD | Docker | Kubernetes | Grafana | Prometheus | Copilot - Conduct : +91 8056166215 | [email protected]
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.