Ninject 2: Is there an equivalent to 1.5's InjectPropertiesWhere?

263 views Asked by At

I am using Ninject 1.5 in my MVC project. It works nicely, but since we have Ninject 2, I could upgrade (and additionaly use per request behavior, which didn't work properly in 1.5). Ninject 1.5 had InjectPropertiesWhere function, which is missing in Ninject 2 (at least it was when I tested it some time ago). Is there something similar?

Example of InjectPropertiesWhere:

return Bind<IUserService>().To<UserService>()
    .InjectPropertiesWhere(p => p.Name.EndsWith("Repository"))
    .InjectPropertiesWhere(p => p.Name.EndsWith("Service"))
    .InjectPropertiesWhere(p => p.Name == "ApplicationCache")
    .InjectPropertiesWhere(p => p.Name == "CurrentPrincipal")
    .InjectPropertiesWhere(p => p.Name == "CTEmailSender")
    .InjectPropertiesWhere(p => p.Name == "CTSettings");
1

There are 1 answers

3
Remo Gloor On BEST ANSWER

This is not supported this way by Ninject 2. You have 4 Options:

  1. Switch to Constructor injection, which is always the prefered way of injection.
  2. Add the Inject Attribute to your properties (Or another attribute and configure it as the inject attribute)
  3. Use WithProperty("propertyName", ctx => ctx.Kernel.Get<MyType>())
  4. The behavior can be added by writing an activation strategy that injects the configured properties. The configuration which properties shall be injected can be added to the bindings metadata using an extension method.