Clean Architecture in a C#

Clean Architecture is a software design pattern that separates the application into independent layers, where each layer has a specific responsibility and interacts with other layers through well-defined interfaces. The main goal of clean architecture is to create a flexible, testable, and maintainable software system.

By following this architecture, we have created a flexible, testable, and maintainable software system. The domain layer is independent of any external dependencies and frameworks, making it easy to test and change without affecting other layers. The application layer contains the use cases or application-specific logic, which is decoupled from the infrastructure

Here's an example of how to implement Clean Architecture in a C# project:

1. Domain Layer: This layer contains the business logic and domain-specific entities. It should be independent of any external dependencies and frameworks.

C#
public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
}

public interface IUserRepository
{
    Task GetById(int id);
    Task Add(User user);
    Task Update(User user);
    Task Delete(int id);
}

2. Application Layer: This layer contains the use cases or application-specific logic that uses the domain layer to perform specific tasks.

C#
public class UserService
{
    private readonly IUserRepository _userRepository;

    public UserService(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }

    public async Task GetUserById(int id)
    {
        return await _userRepository.GetById(id);
    }

    public async Task AddUser(User user)
    {
        await _userRepository.Add(user);
    }

    public async Task UpdateUser(User user)
    {
        await _userRepository.Update(user);
    }

    public async Task DeleteUser(int id)
    {
        await _userRepository.Delete(id);
    }
}

3. Infrastructure Layer: This layer contains the implementations of the interfaces defined in the domain layer, as well as any external dependencies such as databases or web services.

C#
public class UserRepository : IUserRepository
{
    private readonly DbContext _context;

    public UserRepository(DbContext context)
    {
        _context = context;
    }

    public async Task GetById(int id)
    {
        return await _context.Users.FirstOrDefaultAsync(u => u.Id == id);
    }

    public async Task Add(User user)
    {
        await _context.Users.AddAsync(user);
        await _context.SaveChangesAsync();
    }

    public async Task Update(User user)
    {
        _context.Users.Update(user);
        await _context.SaveChangesAsync();
    }

    public async Task Delete(int id)
    {
        var user = await GetById(id);
        _context.Users.Remove(user);
        await _context.SaveChangesAsync();
    }
}

4. Presentation Layer: This layer contains the user interface or presentation logic, which interacts with the application layer to perform specific tasks.

C#
public class UserController : Controller
{
    private readonly UserService _userService;

    public UserController(UserService userService)
    {
        _userService = userService;
    }

    public async Task Index(int id)
    {
        var user = await _userService.GetUserById(id);
        return View(user);
    }

    public async Task Create(User user)
    {
        await _userService.AddUser(user);
        return RedirectToAction(nameof(Index), new { id = user.Id });
    }

    public async Task Edit(User user)
    {
        await _userService.UpdateUser(user);
        return RedirectToAction(nameof(Index), new { id = user.Id });
    }

    public async Task Delete(int id)
    {
        await _userService.DeleteUser(id);
        return RedirectToAction(nameof(Index));
    }
}


Comments

Popular posts from this blog

Send API POST Request in MS SQL Server

Restrict Special Characters Using JavaScript By Copy,Paste

Create Extension Method in typescript - String.IsNullOrEmpty()

Create Custom Form Control for ng-select in Angular

Display PDF Or Tiff Files In IFrame

Export to Excel in Angular 6 | 7 | 8 | 9 | 10

Create a function to convert from string to Decimal using JavaScript

How to Create Custom DialogBox using JQuery

Remove duplicate objects/data from an array of objects in Javascript