I successfully use ASP.NET 5 / MVC 6 DI in my controllers using constructor injection.
Now I have a script in which I want my view models to utilize the service in the Validate method when implementing the IValidatableObject.
Embedding a constructor in a ViewModel does not work because they need a constructor without parameters without parameters. Checking Context.GetService also does not work.
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { MyService myService = (MyService)validationContext.GetService(typeof(MyService));
always causes MyService to be null.
ASP.NET 4, I would create a ValidatableObjectAdapter, register it through DataAnnotationsModelValidatorProvider.RegisterDefaultValidatableObjectAdapterFactory, and then I could use validationContext to reference objects for services.
I am currently using the assembly in the DI container for ASP.NET 5, at some stage I will move on to the structure structure), but it does not matter.
My special check is that the property of the object (like username) is unique. I want to delegate this test to the service level.
source share