Clean Architecture


 We have always heard and talked about N-Tier and N-Layer architecture along with SOLID and SOC, but as the size of application along with complexity has changed over a period of time do has application architecture. Also since teams have adopted agile, releases are much more frequent than they used to be a few years back.

In the light of the changing landscape, it is important to structure your application design in a way to give best practices, manageable code, loosely coupled features/functionality, and the least possible change propagation into various layers of the application.

Popular architecture:-

  • Layered/N-Tiered architecture
  • Monolithic architecture
  • Microservices architecture
  • Event-driven architecture
  • Service-oriented architecture

What is Clean Architecture:-

    Clean architecture is another name for Onion architecture, it can be viewed as an onion that consists of various application layers, can be primarily divided into 2 parts i.e Core and Peripheral layers.



In the below structure of OrderingAPI, the API layer depends on Infrastructure for functionalities, Infrastructure depends on Application and Application depends on Domain.



Domain/Entities Layer:-

The entities/domain layer consists of domain entities and nothing else, it has no dependencies and consists of only  POCO/POJO.

Application Layer:-



This layer consists of various different components as below:

1) Contracts

    The contract layer as the name suggests consists of various contracts like repository interfaces, these interfaces will be implemented in the infrastructure layer.

2)Features

    These are various use cases that we are going to implement in the application. In the case of CQRS, it will consist of all the commands and queries in a structured folder format.



3)Mapping/Model
    This will consist of all DTO(Data transfer objects) and mapping to map it to a corresponding entity, you need DTO so that front-end requirements do not cause an unwanted change in the Domain layer.

Consider a scenario where you need a new property communicated to the UI layer(IsABulkBuyer) this property will not be stored in the database and assigned at runtime by calling a web service.
In such a scenario, you can create a property in DTO and assign it after mapping with the domain entity, here we successfully send a new property with the domain entity unchanged.

4)Behaviour
    This will consist of business rules/validations. This may contain all fluent API validators.

Infrastructure Layer


1)Persistence
    Database context for entities defined in Domain layer.
    


2)Repositories
    Implementing interfaces defined in the Application/Contract layer.
    

3)Any other interface defined for any of the interfaces.

Advantages of Clean Architecture:-
  • Mocking is easy which makes unit testing easier.
  • The database can be easily changed by changing only the infrastructure layer.
  • Very clear division of responsibility between layers.
  • The same architecture can be used in any technology.

Comments

Popular posts from this blog

Gitflow using source tree

RabbitMQ setup and cluster configuration on a windows network

Component analysis for application security

Analyse log using plug and play FileBeat

Introduction to Blazor

.NET MAUI and Blazor to create windows/web and mobile application (.NET 6.0)

What Interview Is/Is Not for an interviewer

Estimation techniques

WCAG Accessibility