Introduction
Implementing the Command Pattern in an ASP.NET Core Web API with Clean Architecture involves creating layers for different concerns like Application, Domain, and Infrastructure. Below is a basic outline to guide you through the process. Note that this is a simplified example, and you may need to adjust it based on your specific requirements.
Step 1. Project Structure
Create the following projects in your solution.
- CarCompany.API: This is your Web API project.
- CarCompany.Application: Application services and command handlers go here.
- CarCompany.Domain: Entities, Aggregates, and Domain Services go here.
- CarCompany.Infrastructure: Repositories, Database Context, and other infrastructure-specific code go here.
Step 2. Define Domain Entities
// Sardar Mudassar Ali Khan
// CarCompany.Domain/Entities/Car.cs
public class Car
{
public int Id { get; set; }
public string Model { get; set; }
}
Step 3. Define Commands
// Sardar Mudassar Ali Khan
// CarCompany.Application/Commands/CreateCarCommand.cs
public class CreateCarCommand: ICommand<int>
{
public string Model { get; set; }
}
public class UpdateCarCommand: ICommand
{
public int Id { get; set; }
public string Model { get; set; }
}
public class DeleteCarCommand: ICommand
{
public int Id { get; set; }
}
Step 4. Define Command Handlers
// Sardar Mudassar Ali Khan
// CarCompany.Application/Handlers/CreateCarCommandHandler.cs
public class CreateCarCommandHandler: ICommandHandler<CreateCarCommand, int>
{
private readonly ICarRepository _carRepository;
public CreateCarCommandHandler(ICarRepository carRepository)
{
_carRepository = carRepository;
}
public async Task<int> Handle(CreateCarCommand request, CancellationToken cancellationToken)
{
var car = new Car { Model = request.Model };
await _carRepository.AddAsync(car);
return car.Id;
}
}
// CarCompany.Application/Handlers/UpdateCarCommandHandler.cs
public class UpdateCarCommandHandler: ICommandHandler<UpdateCarCommand>
{
private readonly ICarRepository _carRepository;
public UpdateCarCommandHandler(ICarRepository carRepository)
{
_carRepository = carRepository;
}
public async Task Handle(UpdateCarCommand request, CancellationToken cancellationToken)
{
var car = await _carRepository.GetByIdAsync(request.Id);
if (car == null)
{
throw new NotFoundException($"Car with ID {request.Id} not found.");
}
car.Model = request.Model;
await _carRepository.UpdateAsync(car);
}
}
// CarCompany.Application/Handlers/DeleteCarCommandHandler.cs
public class DeleteCarCommandHandler: ICommandHandler<DeleteCarCommand>
{
private readonly ICarRepository _carRepository;
public DeleteCarCommandHandler(ICarRepository carRepository)
{
_carRepository = carRepository;
}
public async Task Handle(DeleteCarCommand request, CancellationToken cancellationToken)
{
var car = await _carRepository.GetByIdAsync(request.Id);
if (car == null)
{
throw new NotFoundException($"Car with ID {request.Id} not found.");
}
await _carRepository.DeleteAsync(car);
}
}
Join the conversation! Your thoughts help the community grow.