Câu hỏi phỏng vấn Clean Architecture
Câu hỏi

How shall we integrate DB Layer access in Clean Architecture?

Câu trả lời

How Shall We Integrate DB Layer Access in Clean Architecture?

Integrating the database layer in Clean Architecture involves adhering to the principles of separation of concerns and dependency inversion. The goal is to ensure that the core business logic remains independent of the database and other external systems. Here’s a detailed approach to integrating the database layer in Clean Architecture:

1. Define Interfaces in the Core Layer

The core layer, which includes the domain and application layers, should define interfaces for data access. These interfaces represent the contracts that the infrastructure layer will implement. This ensures that the core layer does not depend on the implementation details of the database.

For example, you might define a repository interface in the domain layer:

public interface ICustomerRepository
{
    Customer GetCustomerById(int id);
    void AddCustomer(Customer customer);
    // Other CRUD operations
}

2. Implement Interfaces in the Infrastructure Layer

The infrastructure layer is responsible for the actual implementation of the data access logic. This layer will implement the interfaces defined in the core layer. The implementation will handle the specifics of interacting with the database, such as using Entity Framework, Dapper, or any other ORM.

For example, an implementation using Entity Framework might look like this:

public class CustomerRepository : ICustomerRepository
{
    private readonly AppDbContext _context;

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

    public Customer GetCustomerById(int id)
    {
        return _context.Customers.Find(id);
    }

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

    // Other CRUD operations
}

3. Use Dependency Injection

To ensure that the core layer remains independent of the infrastructure layer, use dependency injection to inject the repository implementations into the application services or use cases. This is typically configured in the composition root of the application, such as the startup class in an ASP.NET Core application.

For example, in ASP.NET Core:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<AppDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddScoped<ICustomerRepository, CustomerRepository>();

    // Other service registrations
}

4. Data Transfer Objects (DTOs) and Mappings

To avoid leaking database entities into the core layers, use Data Transfer Objects (DTOs) to transfer data between layers. Map the database entities to DTOs in the infrastructure layer before passing them to the core layer.

For example:

public class CustomerService
{
    private readonly ICustomerRepository _customerRepository;

    public CustomerService(ICustomerRepository customerRepository)
    {
        _customerRepository = customerRepository;
...
middle

middle

Gợi ý câu hỏi phỏng vấn

middle

Explain what is Dependency Rule in Clean Architecture

middle

What are Use Cases in Clean Architecture?

middle

What is the role of the Controller in Clean Architecture?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào