.netentity-frameworkvalidationpocovalidation-application-bl

Where/when should i validate my data when using EF and POCOs?


I started a project to see what EF 4 can do with POCOs. I created a db and a custom POCO. Now i want to validate my data. For this i'm using the Enterprise Library Validation Block 5.

I have no problem including the validation in my POCOs through attributes and using it with Entity Framework but this means that my POCOs wouldn't be POCOs anymore because i inserted the dependency to the Validation framework.

Any advice where should i insert the validation part and keep the POCOs clean?


Solution

  • I agree that you would like to keep your entities free from validation. It is not the responsibility (SRP) of the domain object itself.

    Besides attribute based validation, the Enterprise Library Validation Application Block (VAB) also supports configuration based validation. There are two models you can follow here:

    1. Use XML based configuration. This is well supported. The VAB contains a configuration tool that allows you to configure the whole thing without writing a single line of XML. Especially the 5.0 tool is very good. Still, using XML makes it hard to refactor anything in your model (however, unit tests will help you spot error sooner).

    2. Using code based configuration. While I personally like this model, because it can do a much better job in making your domain easy to refactor. You can get it to work, but it isn't a well supported yet. You can look at this thread to see an example of how to code based configuration and what the current short comes are.

    Good luck.