Getting Started with Clean Architecture: A Practical Guide for Beginners

Introduction

Hey there, devs! Let’s talk about one of the coolest and most useful concepts to improve your code: Clean Architecture.

Have you ever found yourself trying to understand messy code or found it hard to add new features without breaking anything?

Clean Architecture is here to save the day!

In this post, I’ll show you the basics of this approach and how it can transform your software projects, making everything more organized and easier to maintain.

Let’s go?

Clean Architecture

What is Clean Architecture?

So, after all, what is this thing called Clean Architecture?

The Clean Architecture is a software design principle introduced by Robert C. Martin, often referred to as “Uncle Bob.”. Basically, it’s a way to organize your code into well-defined layers, each with its own responsability. To maximize the benefits, it is essential to also follow the principles of Clean Code.

This helps keep things separate and independent, which is great for making maintenance and evolution of the software easier.

Basic Independence Principles

Benefits of Clean Architecture

Adopting Clean Arch brings several benefits to software projects:

Layers of Clean Architecture

Clean Architecture is divided into several layers, each with its own responsabilities. Let’s expore each one:

Clean Architecture

Implementing Clean Architecture in C#

Let’s look at a simple, but practical, example of how to implement Clean Architecture in a C# project.

public class Customer
{
    public Customer(Guid id, string name, string email)
    {
        Id = id;
        Name = name ?? throw new ArgumentNullException(nameof(Name));
        Email = email ?? throw new ArgumentNullException(nameof(Email));
    }
    
    public Guid Id { get; init; }
    public string Name { get; init; }
    public string Email { get; init; }
}

public class CreateCustomer
{
    public string Name { get; set; }
    public string Email { get; set; }
}
public interface ICustomerService
{
    Guid Add(Customer customer);
    Customer GetById(Guid id);
}

public class CustomerService : ICustomerService
{
    private readonly ICustomerRepository _customerRepository;

    public CustomerService(ICustomerRepository customerRepository)
    {
        _customerRepository = customerRepository;
    }

    public Guid Add(CreateCustomer request)
    {
        var id = Guid.NewGuid();
        
        var customer = new Customer(id, request.Name, request.Email);
        
        _customerRepository.Add(customer);
        
        return id;
    }

    public Customer GetById(Guid id)
    {
        return _customerRepository.GetById(id);
    }
}
[ApiController]
[Route("customers")]
public class CustomerController : ControllerBase
{
    private readonly ICustomerService _customerService;

    public CustomerController(ICustomerService customerService)
    {
        _customerService = customerService;
    }

    [HttpPost]
    public IActionResult Add([FromBody] CreateCustomer request)
    {
        var id = _customerService.Add(request);
        return Ok(id);
    }

    [HttpGet("{id}")]
    public IActionResult Get(Guid id)
    {
        var customer = _customerService.GetById(id);
        
        if (customer is null)
          return NotFound();

        return Ok(customer);
    }
}
public class CustomerRepository : ICustomerRepository
{
    private readonly AppDbContext _context;

    public CustomerRepository(AppDbContext context)
    {
        _context = context;
    }

    public void Add(Customer customer)
    {
        _context.Customers.Add(customer);
        _context.SaveChanges();
    }

    public Customer? GetById(Guid id)
    {
        return _context.Customers.FirstOrDefault(c => c.Id == id);
    }
}

Best Practices and Tips

To keep your architecture clean and efficient, follow these best practices:

Conclusion

Adopting Clean Architecture can transform the way you develop software, bringing clarity, flexibility, and maintainability to your code. Complementing this approach with the principles of Clean Code can further enhance these benefits.

While it may seem complex at first, constant practice and gradual adoption of its principles and patterns will make this model second nature in your daily work. Try applying these concepts to your projects and see how they can improve the quality and robustness of your software.

Now, you know a lit bit more about Clean Architecture, let me know your thoughts about it. Did you like it?

See ya!