DEV Community

Sachin Saini πŸ¦„
Sachin Saini πŸ¦„

Posted on

10 1

Clean Architecture in Practice

Organizing your project can be as important as the business logic of your app, especially in the long run. Ease of maintenance and integrating new functionality are core parts of the software development lifecycle. So it makes sense to organize your project in such a way that it welcomes change instead of opposing it.

Full example code can be found here.

Clean Architecture (CA)

This is my take on clean architecture, you don’t have to agree with me :)

Clean architecture lays out a set of rules to allow your apps to be easily extensible. Following these rules, you can build systems that are:

  • Independent of frameworks. You can easily swap one framework for another without rewriting your whole app.
  • Testable.
  • Independent of the database. You can swap PostgreSQL for MySQL or a NoSQL database.

Layers in CA

CA is a layered architecture, the diagram below tries to show various layers.

CA Layers

There can be more than 4 layers depending upon your project and the complexity.

Models

Think of a model as an object or data-structure that contains a blueprint of an entity in your business logic.

package user

type User struct {
    Name    string
    ID      int64
    Email   string
}
Enter fullscreen mode Exit fullscreen mode

In the code example above, the User struct contains the blueprint for the user. The same structure applies to all the entities in your business logic.

Repository

The repository is responsible for talking with the persistence layer. We are abstracting it with an interface.

package user

// ...snip

type UserRepository interface {
    GetByID(id int64) (*User, error)
    Update(u *User) error
    Delete(id int64) error
}
Enter fullscreen mode Exit fullscreen mode

Whatever persistence layer you are using in your system be it PostgreSQL or MongoDB, must satisfy this interface. This way we have decoupled the database from our system.

Usecase

This layer implements business-specific logic and communicates with the repository. We are abstracting away the usecase layer with the help of an interface.

package user

// ...snip

type UserUsecase interface {
    GetByID(id int64) (*User, error)
    Update(u *User) error
    Delete(id int64) error
}
Enter fullscreen mode Exit fullscreen mode

The Usecase and the repository happen to be same, it's not a rule.

Delivery

The delivery layer is responsible for the exchange of data between your system and the clients. The communication can take place with the help of a REST API or RPC or any other protocol or many protocols at the same time. the delivery layer is also responsible for serialization/de-serialization of data.

The data should be passed between the layers using only data-structures or as function parameters.

Dataflow

CA Dataflow

Understanding how the data flows in CA can be a bit confusing at first but it's quite simple in practice.

The client makes a request using a delivery protocol supported by your system. The delivery layer de-serializes the data from the request and passes it to the usecase layer. The usecase layer processes the data and talks to the repository if necessary. The processed data is sent up the chain to the delivery layer where it's serialized and sent back to the client.

Useful Links

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay