I'm working on an ASP.NET MVC project. In the project I have a service layer that accepts DTOs for CRUD operations. When I need to validate business logic, should the validator accept DTOs, Entity Models, or something else entirely?
For example:
public class ProductService: IProductService
{
public ValidationResult CreateProduct(ProductDTO productDto)
{
//call productValidator.Validate(productDto) on the DTO here?
Product productEntityModel = mapper.Map<Product>(productDto);
//or, call productValidator.Validate(productEntityModel) on the Entity model here?
if(validationResult.Valid)
{
_dbContext.Products.Add(productEntityModel);
_dbContext.SaveChanges();
}
return validationResult
}
}
Some thoughts:
- I've seen online some talk of creating a POCO which can have validation logic (rather than using a validation service) and even other business logic inside of it. This makes sense, but it is yet one more "representaion" of a product that has to be managed and maintained.
- Validating against the DTO maybe seems a little more reasonable since that is that the caller is sending to the service?
Thanks for the help!!