Introduction
In the realm of modern web development, constructing a robust and scalable solution is paramount. This journey often involves harmonizing architectural principles and design patterns to enhance maintainability and flexibility. Our venture explores the creation of an ASP.NET Core Web API utilizing the Clean Architecture paradigm and harnessing the power of the Proxy Pattern. Focused on the dynamic domain of CarCompany management, our implementation establishes a structured and modular approach to CRUD (Create, Read, Update, Delete) operations.
Through the systematic use of interfaces, repositories, and dependency injection, we aim to not only facilitate seamless data access but also introduce a Proxy layer, offering a versatile gateway for implementing additional functionalities such as caching, logging, and validation. This amalgamation of industry-best practices forms the foundation for a resilient and extensible web API tailored for CarCompany data management.
Proxy Pattern in an ASP.NET Core Web API with Clean Architecture. The example will focus on a CarCompany CRUD (Create, Read, Update, Delete) application.
1. Define the Domain Model
// Sardar Mudadssar Ali Khan
// Domain Layer
public class CarCompany
{
public int Id { get; set; }
public string Name { get; set; }
}
2. Create an Interface for the Repository
// Sardar Mudassar Ali Khan
// Data Access Layer
public interface ICarCompanyRepository
{
Task<CarCompany> GetByIdAsync(int id);
Task<List<CarCompany>> GetAllAsync();
Task<int> CreateAsync(CarCompany carCompany);
Task UpdateAsync(CarCompany carCompany);
Task DeleteAsync(int id);
}
3. Implement the Repository
// Sardar Mudassar Ali Khan
// Data Access Layer
public class CarCompanyRepository: ICarCompanyRepository
{
private readonly ApplicationDbContext _dbContext;
public CarCompanyRepository(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<CarCompany> GetByIdAsync(int id)
{
return await _dbContext.CarCompanies.FindAsync(id);
}
public async Task<List<CarCompany>> GetAllAsync()
{
return await _dbContext.CarCompanies.ToListAsync();
}
public async Task<int> CreateAsync(CarCompany carCompany)
{
_dbContext.CarCompanies.Add(carCompany);
await _dbContext.SaveChangesAsync();
return carCompany.Id;
}
public async Task UpdateAsync(CarCompany carCompany)
{
_dbContext.Entry(carCompany).State = EntityState.Modified;
await _dbContext.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
var carCompany = await _dbContext.CarCompanies.FindAsync(id);
if (carCompany != null)
{
_dbContext.CarCompanies.Remove(carCompany);
await _dbContext.SaveChangesAsync();
}
}
}
4. Create a Proxy for the Repository
// Sardar Mudassar Ali Khan
// Application Layer
public class CarCompanyRepositoryProxy : ICarCompanyRepository
{
private readonly ICarCompanyRepository _realRepository;
public CarCompanyRepositoryProxy(ICarCompanyRepository realRepository)
{
_realRepository = realRepository;
}
public async Task<CarCompany> GetByIdAsync(int id)
{
return await _realRepository.GetByIdAsync(id);
}
public async Task<List<CarCompany>> GetAllAsync()
{
return await _realRepository.GetAllAsync();
}
public async Task<int> CreateAsync(CarCompany carCompany)
{
return await _realRepository.CreateAsync(carCompany);
}
public async Task UpdateAsync(CarCompany carCompany)
{
await _realRepository.UpdateAsync(carCompany);
}
public async Task DeleteAsync(int id)
{
await _realRepository.DeleteAsync(id);
}
}
Join the conversation! Your thoughts help the community grow.