user1838662
user1838662

Reputation: 523

MVC data annotations on model without mvc dependency?

Lets say I have a project that contains all of my model classes, with a separate project containing an MVC 4 web application, (service,repository layers too).

If I want to use MVC specific data annotations for my model properties, how do i do this without having a dependency to MVC?

Any non MVC projects referencing the model would also require the MVC references. I would prefer that these are only added when the models are used by MVC and not everything.

Initially i was thinking partial classes but i know these cannot be used cross assembly.

Making clone classes and inheriting from the originals outside the model classes might not work. It would require the code to refer to these child classes instead of the originals.

I would prefer a more unobtrusive manner of attaching attributes to the model.

Upvotes: 2

Views: 722

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

If I want to use MVC specific data annotations for my model properties, how do i do this without having a dependency to MVC?

By using view models. View models are classes that you design and define in your MVC application and which are the classes that get passed to your views. You should never pass your models to the views. Your controller actions could query your service layer for domain models, then map those domain models to view models and pass those view models to the views. On the other hand your [HttpPost] controller actions should take view models wit htheir respective data annotations as arguments, map those view models to their corresponding domain models and pass the domain models to your service layer.

Upvotes: 6

Related Questions