1

We are building our application by using asp.net 5 and the EF7 with the Onion architecture. We do not know how to validate the entities before they are saved in the database in our Service layer.

Our structure is like this;

CORE

  • Domain.Entities (POCOs generated by EF7)
  • Dto (Pure C# classes without annotations)

Infrastructure

  • Data (We only have the DbContext here, we do not use repositories)
  • Service (Uses EF7 as repository and do all of the database operations. Maps the Domain.Entities POCOs to DTO and vise versa with Automapper. Returns DTO results)

Presentation

  • Models (Similar to DTO but with validation attributes)
  • Web (MVC 6 project. The controller Consumes the Service and map the Models to DTOs and vice-versa and do the validation via Models)

Our problem is we don't know how to handle the validation in the Service layer. EF7 does not validate the entity as it used to be in the old versions (https://github.com/aspnet/EntityFramework/wiki/Entity-Framework-Design-Meeting-Notes---July-17,-2014). We do not want to have another DTO/Model like layer for the Service to place validations in it as we have our Model project in the Presentation layer.

What would be the best approach?

Edit:

We have tried to use DbEntityValidationException like in EF7 to handle the validation via EF but we found out that it is not possible. we have also tried to have validation classes for each DTO in our service layer but it didn't sound right to us as it adds complexity to the coding. We have also tried following method; We generated the entities with data annotations by the reveng tool. And we validated the entity classes like following;

public class EntityBase 
{   
    public virtual DateTime CreatedDate { get; set; }
    public virtual string CreatedBy { get; set; }
    public virtual DateTime ModifiedDate { get; set; }
    public virtual string ModifiedBy { get; set; }
    public int RowVersion { get; set; }

}
public class MyEntity
{   
    public virtual int MyEntityId { get; set; }
    [MaxLength(50)]
    public virtual string Name { get; set;}

}
public static class EntityValidator
{
    public static ICollection<ValidationResult> Validate(this EntityBase entity)
    {
        if (entity == null) return new List<ValidationResult>();

        var validationErrors = new List<ValidationResult>();
        var context = new ValidationContext(entity, null, null);
        var isValid = Validator.TryValidateObject(entity, context, validationErrors,true);
        return validationErrors;
    }
}

so that allows us to use it like; var errors = myEntity.Validate(); in our service layer.

We are still looking for a better solution.

Adem Tas
  • 323
  • 1
  • 9
  • I tried to handle the validation by the EF first and I found out that EF does not validate the models anymore. (https://github.com/aspnet/EntityFramework/wiki/Entity-Framework-Design-Meeting-Notes---July-17,-2014). I also tried to add validation classes for each DTO in the service layer but it didn't sound right to me. I am looking for the ideal solution for our situation here. – Adem Tas Dec 07 '15 at 22:05
  • 1
    I would recommend you to read two fantastic answers for questions like you have about validation: [this](http://stackoverflow.com/questions/4776396/validation-how-to-inject-a-model-state-wrapper-with-ninject/4851953#4851953) and [this](http://stackoverflow.com/questions/16793982/separating-the-service-layer-from-the-validation-layer). Hope these might give you answers. – kayess Dec 12 '15 at 17:36

0 Answers0