How do I get property injection working in Ninject for a ValidationAttribute in MVC?

Posted by jaltiere on Stack Overflow See other posts from Stack Overflow or by jaltiere
Published on 2010-04-29T19:04:53Z Indexed on 2010/04/29 19:07 UTC
Read the original article Hit count: 276

Filed under:
|

I have a validation attribute set up where I need to hit the database to accomplish the validation. I tried setting up property injection the same way I do elsewhere in the project but it's not working. What step am I missing?

public class ApplicationIDValidAttribute : ValidationAttribute
{
    [Inject]
    protected IRepository<MyType> MyRepo;

    public override bool IsValid(object value)
    {   
       if (value == null)
         return true;

       int id;
       if (!Int32.TryParse(value.ToString(), out id))
         return false;

       // MyRepo is null here and is never injected
       var obj= MyRepo.LoadById(id);
       return (obj!= null);
    }

One other thing to point out, I have the Ninject kernel set up to inject non-public properties, so I don't think that is the problem. I'm using Ninject 2, MVC 2, and the MVC 2 version of Ninject.Web.MVC.

Thanks!

© Stack Overflow or respective owner

Related posts about ninject-2

Related posts about asp.net-mvc-2