0
votes

I'm using an Interface to define my object in my MVC Controller, and using a Dependency Injection framework to inject various different concrete implementations.

The validation for the object is using DataAnnotations, but here comes the problem.

Because I'm using an Interface for the model which gets injected in to the controller, the DataAnnotations have to be against the interface, which is wrong because each concrete implementation that is being injected has its own DataAnnotations validation rules.

How can I ensure that the injected models DataAnnotations are being used when perform validation?

1
You have a big trouble! I think that you can archive this only using IValidatableObject, so every object will validate it self! I don't think that is possible to inject DataAnnotations without some complex hard coding! - Fals

1 Answers

1
votes

You should inject repositories into your controller using a DI framework, not models. Models should not be injected. Models is something that you retrieve/update through the repository interface that gets injected. The validation is performed on the models.