I have a very simple 3-tier layer ASP.NET Core WebAPI application:
- Domain - with
Entities,Data interfaces,ServicesandDTOobjects (which are the only input and output objects). So "the interface of the Domain" are Services that accepts input DTOs and returns output DTOs. - Infrastructure - Implementation of the
Data interfaces(Data repositories) and CodeFirst Migrations. - WebAPI - Standard ASP.NET Core 3.1 Web API project.
The WebAPI project returns "Output" DTOs in Controllers. For endpoints that accepts payload, "Input" DTOs are used. Controllers are very much similar to the Services from the Domain. Controllers expose Domain Services to the world (obviously, the interface of the Domain).
But then comes the Validation... I'm familiar with FluentValidation and with the ASP.NET Core plumbing - it's an awesome middleware:
services.AddControllers()
.AddFluentValidation(opt =>
{
opt.RegisterValidatorsFromAssemblyContaining(typeof(PersonInputValidator));
});
I implement validation for each "Input" DTO and this works great, BUT... I'm not sure if this is enough. If you take any Service class, it's pretty much not validated. It's .NET Core middleware that technically validates the input to the Controller.
Should I "double-validate" within Services again? If so, is there a smooth way to reuse Validators I already have?