Reputation: 105
Could someone point me in the direction of how to create a custom "Inject" attribute with Ninject?
I would like to do the following:
When a property with this attribute is to be injected, the injected value is to be loaded from a "ViewManager" class.
I found an example with ISelector to decide if the property / filed is to be injected, however I couldn't find out how to add a custom "injection strategy" for this - I would like to delegate the injection of the actual value to my ViewManager.
Upvotes: 1
Views: 1187
Reputation: 105
I solved this by adding a custom IInjectionHeuristic which allows injecting by my custom attribute. Then I also added a custom IBindingResolver which adds an additional binding per type that is resolved - this binding has a condition checking for the custom attribute, so that it doesn't break any previous bindings...
Tha bindings created by the custom IBindingResolver sets a local "ProviderCallback", which utilizes the extracted property and passes the request to an internal ViewRegionManager instance.
Hope this helps in case anyone wants to do something similar in the future.
Upvotes: 3
Reputation: 32725
It's already supported and can be done with a simple configuration of the kernel.
new StandardKernel(new NinjectSettings() { InjectAttribute = typeof(MyOwnInjectAttribute) };
Upvotes: 4