From Patchwork to Craftsmanship: Building a Full-Stack Web App in ASP.NET Core with Clean Architecture (Part 2)

edinstation
3 min readOct 11, 2023

--

Architecture is defined in the construction industry as the study, planning, and building of a range of structures that satisfy both functional and visual requirements. With the same idea in mind, a software system’s architecture acts as a blueprint. It’s a methodical approach to structuring and creating code that controls complexity, determines how components communicate with one another, and is in line with both technical and business objectives.

The Significance of Clean Architecture

Consider the following example: Would you build your dream home without architectural plans? Equally, software architecture is an important step in the development process of your desired application. As Uncle Bob (Author of Clean Code) clearly stated,

architecture is about intent rather than the tools or frameworks utilised. It should give a clear structure that expresses the project’s goal without getting stuck in the implementation details.

Photo by Anete Lusina: https://www.pexels.com/photo/floor-plan-hanging-on-whiteboard-4792491/

A brief look at the diagram above reveals the structure’s nature immediately, leaving no space for doubt. This highlights one of the fundamental goals of clean architecture. Similarly, regardless of the tools or programming languages used, the software project’s top-level directory structure should convey the project’s goal effortlessly.

What Exactly is Clean Architecture?

Clean architecture is a systematic approach to organising and structuring code components in a way that promotes maintainability, scalability, and separation of concerns.

It focuses on enforcing the dependency rule, which effectively means that all source code dependencies should always point inward, resulting in a well-organised and maintained codebase. It offers several benefits, including:

  • Dependency Rule: Dependencies should always point inward, with high-level modules not dependent on low-level ones.
  • Abstraction over Implementation: Code should be structured to reveal intent, not implementation details.
  • Testability: Components should be designed for easy testing, aiding in the development of robust applications.
  • By following clean architecture principles, you can create maintainable, scalable, and flexible software systems that meet both business and technical objectives.

Organising Code in Clean Architecture

Clean architecture separates the codebase into three basic layers: the presentation / web / ui layer, the infrastructure layer, and the application core, which consists of the domain and application. The diagram below offers a detailed representation of this architecture.

Core Layer (Application + Domain)

The application core consist of both domain and application layers

Domain

The “Domain” layer is the software system’s heart, containing the essential business logic, entities, and domain-specific rules. The dependence rule can be rephrased to state that all other layers revolve around and rely on the Domain layer. This layer contains:

  • Entities: classes that can be persisted in the database.
  • Aggregates: grouped entities

Application

The “Application” layer in the diagram is visually emphasised with an outer red semi-circle, although coexisting with the “Domain” layer as the system’s core. This is due to the fact that the application layer and infrastructure layer cannot communicate directly. This layer contains:

  • Interfaces: abstractions for operations performed in other layers.
  • Services: services specific to the domain.
  • Custom Exceptions: user defined exceptions within the domain.
  • Domain Events & Handlers: domain-level events and their handlers.

Infrastructure

Implementations for data access reside at the infrastructure layer. Interactions with services in the “Application” layer are done through interfaces defined in the core as the application layer and infrastructure layer cannot communicate directly as seen in the diagram above.

Although this example is exclusive to ASP.NET Core, the basic idea is applicable to many frameworks. The following can be found in the infra layer:

  • EF Core Types: Such as DbContext and EF Core Migrations.

Presentation/ Web/ UI

This is the entry point of your application, responsible for interacting with users. Key components include:

  • Controllers: Handle user requests and manage responses.
  • Views: Render user interfaces.

Conclusion

In the next article of this series, we will embark on a practical journey. We’ll roll up our sleeves and get to work on our own project, putting the principles we’ve learned so far into practise. Keep an eye out for the next exciting chapter in our Clean Architecture journey!

--

--

edinstation

Immerse yourself in technology, software, animation and extended reality. Explore our digital realm. 🚀"