Question

What's the best way of implementing xVal when using the Entity Framework? I've been using this approach but I'd like to know if there's a better way of doing it.

Also I'd like to know if there's a way of doing it without having to raise an exception.

Was it helpful?

Solution

The metadata approach works well, but the approach that I've used is to have a separate set of ViewModel objects and use a tool like AutoMapper to map from the EF objects to the ViewModel objects. (In more complex implementations, there is a separate domain model in between the EF objects and ViewModel objects.)

If you implement a repository pattern to retrieve your ViewModel objects, it also makes testing your controllers a lot easier to do.

You can then attribute your ViewModel objects to your heart's content and not worry about codegen overwriting your attributes. It's also possible to use another validation method (e.g. FluentValidation), since these can be made to work smoothly with xVal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top