0% found this document useful (0 votes)
55 views

Clean Architecture With .NET and .NET Core — Overview by Ashish Patel .NET Hub Medium

The document provides an overview of Clean Architecture in the context of .NET and .NET Core, emphasizing the separation of concerns among different layers: Domain, Application, Infrastructure, and User Interface. It explains how business logic is organized within the Domain and Application layers, while the Infrastructure layer implements the necessary interfaces and dependencies. The document also outlines the structure and components of each layer, highlighting the importance of maintaining independence from external frameworks and libraries.

Uploaded by

mksnow48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Clean Architecture With .NET and .NET Core — Overview by Ashish Patel .NET Hub Medium

The document provides an overview of Clean Architecture in the context of .NET and .NET Core, emphasizing the separation of concerns among different layers: Domain, Application, Infrastructure, and User Interface. It explains how business logic is organized within the Domain and Application layers, while the Infrastructure layer implements the necessary interfaces and dependencies. The document also outlines the structure and components of each layer, highlighting the importance of maintaining independence from external frameworks and libraries.

Uploaded by

mksnow48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Search Medium

You have 2 free member-only stories left this month. Sign up for Medium and get an extra one.

Member-only story

Clean Architecture with .NET and .NET Core —


Overview
Ashish Patel · Follow
Published in .NET Hub
5 min read · Sep 2, 2021

Listen Share

Introduction to Clean Architecture with ASP.NET Core: Getting started!


Clean Architecture Overview
With Clean Architecture, the Domain and Application layers are at the center of the
design which is known as the Core of the system. Business Logic places into these
two layers, while they contain different kinds of business logic. They are seen as
details and the business layers should not depend on Presentation and
Infrastructure layers. 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 layer. This functionality is
Clean Architecture
achieved by defining abstractions, or interfaces, in the Application layer, which are
then
TL;DRimplemented by types defined in the Infrastructure layer. A common way of
visualizing this architecture
Clean Architecture is on
style focus to use a series
a loosely of concentric
coupled circles, similar
implementation of use to an
cases.
onion architecture.
Use cases as central organizing structure, decoupled from frameworks and
technology details.
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 a 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.

All dependencies flow inwards and Core has no dependency on any other layer.
Infrastructure and Presentation depend on Core, but not on one another.

Domain Layer
Domain Layer implements the core, use-case independent business logic of the
domain/system. By design, this layer is highly abstracted and stable. This layer
contains a considerable amount of domain entities and should not depend on
external libraries and frameworks. Ideally it should be loosely coupled even to the
.NET Framework.

Domain project is core and backbone project. It is the heart and center project of
the Clean Architecture design. All other projects should be depended on the Domain
project.

This package contains the high level modules which describe the Domain via
Aggregate Roots, Entities and Value Objects.

Domain layer contains:

• Entities

• Aggregates

• Value Objects

• Domain Events

• Enums

• Constants

Application Layer
Application Layer implements the use cases of the application based on the domain.
A use case can be thought as a user interaction on the User Interface (UI). This layer
contains all application logic. It 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.

Application layer contains the application Use Cases which orchestrate the high
level business rules. By design the orchestration will depend on abstractions of
external services (e.g. Repositories). The package exposes Boundaries Interfaces (in
other terms Contracts or Ports or Interfaces) which are used by the User Interface.

For example, if the application need to access a email service, a new interface would
be added to application and an implementation would be created within
infrastructure.

Application layer contains:

• Abstractions/Contracts/Interfaces

• Application Services/Handlers

• Commands and Queries

• Exceptions

• Models (DTOs)

• Mappers

• Validators

• Behaviors

• Specifications

Infrastructure Layer
This layer is responsible to implement the Contracts (Interfaces/Adapters) defined
within the application layer to the Secondary Actors. Infrastructure Layer supports
other layer by implementing the abstractions and integrations to 3rd-party library
and systems.
Infrastructure layer contains most of your application’s dependencies on external
resources such as file systems, web services, third party APIs, and so on. The
implementation of services should be based on interfaces defined within the
application layer.

If you have a very large project with many dependencies, it may make sense to have
multiple Infrastructure projects, but for most projects one Infrastructure project
with folders works fine.

• Infrastructure.Persistence
- Infrastructure.Persistence.MySQL
- Infrastructure.Persistence.MongoDB

• Infrastructure.Identity

Infrastructure layer contains:

• Identity Services

• File Storage Services

• Queue Storage Services

• Message Bus Services

• Payment Services

• Third-party Services

• Notifications
- Email Service
- Sms Service

Persistence Layer
This layer handles database concerns and other data access operations. By design
the infrastructure depends on Application layer. This project contains
implementations of the interfaces (e.g. Repositories) that defined in the Application
project.

For instance an SQL Server Database is a secondary actor which is affected by the
application use cases, all the implementation and dependencies required to
consume the SQL Server is created on infrastructure (persistence) layer.

For example, if you wanted to implement the Repository pattern you would do so by
adding an interface within Application layer and adding the implementation within
Persistence (Infrastructure) layer.

Persistence layer contains:

• Data Context

• Repositories

• Data Seeding

• Data Migrations

• Caching (Distributed, In-Memory)

User Interface (Web/Api) Layer


This layer is also called as Presentation. Presentation Layer contains the UI
elements (pages, components) of the application. It handles the presentation (UI,
API, etc.) concerns. This layer is responsible for rendering the Graphical User
Interface (GUI) to interact with the user or Json data to other systems. It is the
application entry-point.

User Interface layer depends on both the Application and Infrastructure layers,
however, the dependency on Infrastructure is only to support dependency injection.
This layer can be an ASP.NET Core Web API with Single Page Application (SPA like
Angular, React) or it can be an ASP.NET Core MVC with Razor Views.

This layer receives HTTP Requests from users, and Presenters converts the
application outputs into ViewModels that are rendered as HTTP Responses.

User Interface layer contains:


• Controllers

• Views

• View Models

• Middlewares

• Filters/Attributes

• Web/API Utilities

Clean Architecture Project Structure

Useful NuGet Packages


• Entity Framework Core

• MediatR

• FluentValidation

• AutoMapper

• Autofac

• Serilog

• Swashbuckle (Swagger)

• Hangfire

• xUnit

View more from .NET Hub

• Top .NET (ASP.NET Core) Open-Source Projects

• Most popular .NET Libraries every developer should know

• Use MediatR in ASP.NET or ASP.NET Core

• Use FluentValidation in ASP.NET or ASP.NET Core

• Use AutoMapper in ASP.NET or ASP.NET Core

• Use Autofac IoC Container in ASP.NET or ASP.NET Core

Happy Coding!!!

Clean Architecture Software Architecture Dotnet Dotnet Core Aspnetcore


Follow

Written by Ashish Patel


8.2K Followers · Editor for .NET Hub

Cloud Architect • 3x AWS Certified • 6x Azure Certified • 1x Kubernetes Certified • MCP • .NET • Terraform •
GCP • OCI • DevOps •(https://bit.ly/iamashishpatel)

More from Ashish Patel and .NET Hub

Ashish Patel in Awesome Cloud

AWS — Difference between Amazon Aurora and Amazon RDS


Comparison: Amazon Aurora vs Amazon RDS.

· 7 min read · Feb 6, 2022

744 1
Ashish Patel in .NET Hub

Use MediatR in ASP.NET or ASP.NET Core


Getting started with MediatR — How to use MediatR in .NET and .NET Core?

· 3 min read · Aug 3, 2021

225 5
Ashish Patel in .NET Hub

Top .NET (ASP.NET Core) Open-Source Projects


Awesome useful .NET Projects on GitHub — Most popular .NET Projects to get practical
knowledge.

· 5 min read · Feb 26, 2022

924 5
Ashish Patel in Awesome Cloud

AWS—Difference between Application load balancer (ALB) and Network


load balancer (NLB)
ALB vs NLB in AWS — Application load balancer vs Network load balancer

· 5 min read · Feb 25

1.6K 12

Recommended from Medium

See all from Ashish Patel

See all from .NET Hub

Justin Muench in CodeX

ASP.NET Core 6 — Logging


Logging is a critical part of any application development process, and ASP.NET Core 6 makes it
easier than ever to implement logging…

· 14 min read · Dec 14, 2022

95
Alex Maher

10 tools for .NET developers that you probably didn’t know about
NCrunch

· 8 min read · Jan 24

321 5

Lists

Stories to Help You Grow as a Software Developer


19 stories · 102 saves
Fuji Nguyen in Knowledge Pills

How to use AutoMapper in C# Net Core?


AutoMapper is a library in C# that allows mapping between objects of different types. It can be
used to convert between different data…

· 4 min read · Jan 27

1
Alex Maher

10 Essential Patterns for C# and .NET Development


Design patterns are reusable solutions to common problems that arise in software design.
They are templates for solving design problems…

· 13 min read · Feb 4

296 6

Vijini Mallawaarachchi in Towards Data Science

10 Common Software Architectural Patterns in a nutshell


Ever wondered how large enterprise scale systems are designed? Before major software
development starts, we have to choose a suitable…

· 5 min read · Sep 4, 2017

38K 125
Shawn Shi in Geek Culture

REST API Best Practices—Implement Design Patterns for Maintainable


web APIs in ASP.NET Core
Discuss how to build REST API endpoints in ASP.NET Core and implement the best design
patterns at the code level.

· 5 min read · Jan 25

163

See more recommendations

You might also like