DEV Community

Rex
Rex

Posted on

Clean Architecture vs Vertical Slice Architecture

I was asked about clean architecture and vertical slice architecture during an interview, which caught me off guard. I use Clean Architecture, DDD and MediatR for my backend, but I had not heard about vertical slice architecture before. We learn a lot from interviews, we definitely should do more of these.

I was curious about vertical slice architecture, so I looked up. Ironically, it turns out that my application layer in Clean Architecture is all about Vertical Slice Architecture(VSA). The use of MediatR enforces VSA. MediatR and VSA have the same father, the famous Jimmy Bogard.

MediatR is the library to help encapsulate each HTTP request into a single handler that takes a request and returns a response. The handler is a black box and does the necessary thing to produce a response, just like a pure function(strictly speaking, only queries).

The result is vertically slicing the application into handlers. The main benefit of this approach is that requests(use cases) are isolated, input/output based and self-contained units like Lego pieces. It makes the code base simple, flat and easy to maintain.

In my application(epicERP.app), I use both Clean Architecture and Vertical Slice Architecture.

  1. Clean Architecture:

    • I have Domain and Application layer hosting all the business logic.
    • The Presentation Layer is a web API layer, but the controllers have no logic. All they are doing are calling handlers inside Application Layer.
    • The infrastructure depends on the Application Layer via interface Inversion of Control.
    • Domain-Driven Design principle is implemented, i.e. domain logic is pushed down to the domain layer.
    • I do not have any services or a service layer, which is the main difference from traditional onion layered architecture.
  2. Vertical Slice Architecture: my Application Layer consists of self-contained commands and queries; they usually inject a Unit of Work that contains repositories. Through the repository, the handlers have access to the database.

Implementing Clean Architecture, Vertical Slice Architecture and DDD together made my application extremely easy to maintain because use cases are translated into independent Lego pieces. Most of them are simple and stupid.

If you are curious about Clean Architecture and Vertical Slice Architecture, I recommend these videos:
Vertical Slice Architecture, Jimmy Bogard's presentation back in 2018. The Pipeline Behaviours mentioned in this video are very interesting.
Clean Architecture, by Jason Taylor.

Top comments (0)