Introduction
Implementing the Decorator Pattern in an ASP.NET Core Web API with Clean Architecture involves structuring your application into layers and using decorators to add functionality to specific methods or classes. Here's a simplified example of a CarCompany CRUD operation using Clean Architecture. This example assumes you already have a basic understanding of Clean Architecture and ASP.NET Core.
Create Solutions and Projects
Start by creating a new ASP.NET Core Web API solution with projects for each layer.
- CarCompany.Application (Application Layer)
- CarCompany.Domain (Domain Layer)
- CarCompany.Infrastructure (Infrastructure Layer)
- CarCompany.WebApi (Presentation Layer)
Define the Domain Model
In the CarCompany.Domain project, define the Car entity.
// CarCompany.Domain/Entities/Car.cs
namespace CarCompany.Domain.Entities
{
public class Car
{
public int Id { get; set; }
public string Model { get; set; }
}
}
Create Repository Interface
Define a repository interface in the CarCompany.Domain project.
// CarCompany.Domain/Interfaces/ICarRepository.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using CarCompany.Domain.Entities;
namespace CarCompany.Domain.Interfaces
{
public interface ICarRepository
{
Task<IEnumerable<Car>> GetAllAsync();
Task<Car> GetByIdAsync(int id);
Task<int> AddAsync(Car car);
Task UpdateAsync(Car car);
Task DeleteAsync(int id);
}
}
Implement Repository
Create a repository implementation in the CarCompany.Infrastructure project.
// CarCompany.Infrastructure/Repositories/CarRepository.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using CarCompany.Domain.Entities;
using CarCompany.Domain.Interfaces;
using Microsoft.EntityFrameworkCore;
public class CarRepository : ICarRepository
{
private readonly YourDbContext _dbContext;
public CarRepository(YourDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<IEnumerable<Car>> GetAllAsync()
{
return await _dbContext.Cars.ToListAsync();
}
public async Task<Car> GetByIdAsync(int id)
{
return await _dbContext.Cars.FindAsync(id);
}
public async Task<int> AddAsync(Car car)
{
_dbContext.Cars.Add(car);
await _dbContext.SaveChangesAsync();
return car.Id;
}
public async Task UpdateAsync(Car car)
{
_dbContext.Entry(car).State = EntityState.Modified;
await _dbContext.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
var car = await _dbContext.Cars.FindAsync(id);
if (car != null)
{
_dbContext.Cars.Remove(car);
await _dbContext.SaveChangesAsync();
}
}
}
Implement Application Service
Create an application service in the CarCompany.Application project.
// CarCompany.Application/Services/CarService.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using CarCompany.Domain.Entities;
using CarCompany.Domain.Interfaces;
public class CarService : ICarService
{
private readonly ICarRepository _carRepository;
public CarService(ICarRepository carRepository)
{
_carRepository = carRepository;
}
public async Task<IEnumerable<Car>> GetAllAsync()
{
return await _carRepository.GetAllAsync();
}
public async Task<Car> GetByIdAsync(int id)
{
return await _carRepository.GetByIdAsync(id);
}
public async Task<int> AddAsync(Car car)
{
return await _carRepository.AddAsync(car);
}
public async Task UpdateAsync(Car car)
{
await _carRepository.UpdateAsync(car);
}
public async Task DeleteAsync(int id)
{
await _carRepository.DeleteAsync(id);
}
}
Join the conversation! Your thoughts help the community grow.