What is clean architecture and why you should use it?

Clean Arch

Introduction

Being a developer, have you ever written code that

  • Is hard to understand because its intent or purpose was not clear?
  • Has logics mixed?
  • Is hard to debug?
  • Is compiling fine but full of logical bugs?
  • Painful to test?

If the answer is “Yes I have”, you are reading the right blog.

Being progressive developers, we learn various software design patterns and beyond that from the GoF Book and a variety of sources. We make deliberate efforts to implement solid principles as we code. All this has always helped us confront the above issues but only to a certain extent. We still see data access and business logic put in controllers, domain models being used everywhere and not being able to test the code without a database and we always feel the scope of improvement in the code.

The solution to all these common development issues is implementing “Clean Architecture” in our code.

Clean architecture is a concept proposed by Uncle Bob in his book Clean Architecture as a way of building highly flexible and maintainable software solutions

What is Clean Architecture?

“Dependency Rule” is the heart of “Clean Architecture” stating that source code dependency can only point inwards towards the application core.

This architecture has been called many names over the years, first name was Hexagonal Architecture, followed by Ports-and-Adapters. Until recently, it’s been cited as the Onion Architecture or Clean Architecture.

Applications that follow the Dependency Inversion Principle as well as the Domain-Driven Design (DDD) principles tend to arrive at a similar architecture and normally contain the following layers:

  • Domain Layer
  • Application Layer
  • Data Layer
  • IoC Layer
  • Presentation Layer
  • Test Layer

How it works?

Clean architecture puts the business logic (Application) and application model (Domain) at the centre of the application together called as Core.

The Core has to be completely independent of data access and other infrastructure concerns, so we invert the dependencies. This is achieved by adding interfaces or abstractions in Core that are implemented by layers outside of Core. For example, we need to implement the Repository Pattern we would add an interface within Core and add the implementation within Infrastructure.Data.

All dependencies flow inwards and Core has no dependency on any other layer. 

Infrastructure.Data and Presentation depend on Core, but not on one another.

How to implement this?

  • Domain: Contains Entities (Business Models) and Repository Interfaces.
  • Application: Contains Service Interfaces and their Services, DTOs (Data Transfer Objects) and View Models.
  • Data: Contains EF Core types (DbContext, Migration), Data access implementation types (Repositories),Infrastructure-specific services (for example, FileLogger or SmtpNotifier).
  • Infrastructure.IoC:  Contains Dependency Container class to help implement Dependency Inversion.
  • User Interface Types: Includes Controllers, Filters, Views, Startup.
  • Tests Types: Includes Unit Tests, Integration Tests.

What are the Benefits of Clean Architecture?

  • Framework Independent – You can use clean architecture with ASP.NET (Core), Java, Python, etc. It doesn’t rely on any software library or proprietary codebase.
  • Database Independent-Majority of your code has no knowledge of what database, if any, is being used by the application. Often, this info will exist in a single class, in a single project that no other project references.
  • UI Independent-The UI project cares about the UI only. It has nothing to do with the implementation of business or data logic.
  • Highly Testable– Apps built using this approach, and especially the core domain model and its business rules, are extremely testable.

At PECS, we promote Clean Architecture across our architecture and delivery teams. Architecture focus has allowed us to deliver high performing scalable software development for more than 20 years. If your application is suffering from performance issues, the reason could be sub-optimal architecture. Feel free to give PECS a call to discuss your challenges and requirements and we’ll be more than happy to share our experiences and assist.

8 comments

  1. Deepak Kumar
    June 23, 2020 at 2:20 pm

    Excellent define the importance of clean Architecture.

    1. Admin
      June 23, 2020 at 5:36 pm

      Thank You for your support!

  2. Dharm Vrat
    June 25, 2020 at 11:55 am

    This is very helpfull for the Developers.

    1. Admin
      June 25, 2020 at 6:37 pm

      We are glad that it was helpful!

  3. Sameer Khan
    June 26, 2020 at 1:24 pm

    good explanation and understandable for developers we have to work as same as ‘How it works?’

    1. Admin
      June 26, 2020 at 5:15 pm

      Right, and also helps in delivering scalable software.

  4. Pankaj Saini
    June 26, 2020 at 3:32 pm

    Great blog which helps to understand The Clean Architecture things…
    Good architecture is the key to create a clean, understandable and reusable code. And it is very helpful to build a Quality, User friendly Interface and high performance application. The Clean architecture code will be Reusable, Clean, Testable and less dependent of the technologies.
    Anyone can easily understand complete work flow, dependencies & constraints and business logic. A tester can write good test cases and scenarios. Users loves to work with this type of Application.

    1. Admin
      June 26, 2020 at 8:12 pm

      Yes, indeed that explains the scope.

Leave a Reply