MasterMastic
MasterMastic

Reputation: 21286

DI Factory for WPF

I'm looking for a way to inject dependency without calling Ninject's Get method.

For example in ASP.NET MVC I create a class that inherits from DefaultControllerFactoryMSDN, and add it to the controller builder's factory with ControllerBuilder.Current.SetControllerFactoryMSDN. Once I do that, I never call Get, and it fills in automatically.

Is there an equivalent for WPF?

I need a general answer that will fit most situations but if I have to give an example, I made a control in WPF, However it has a parameter in the constructor:

public AppVolumeSlider(IVolumeSetter volumeSetter) 

Since my controls are in markup, WPF complains, of course: The type "AppVolumeSlider" does not include any accessible constructors..

How can I make Ninject/WPF fill it appropriately?

Note 1: If I can't do it in this example (I guess because of the markup) please give me a general answer. For example if I just make a class with a similar constructor and not a control. I still need to know this information.

Note 2: I have a hunch this might be related to the Inject attribute, but I have no idea.

Thank you.

Upvotes: 2

Views: 494

Answers (2)

Vladimir Perevalov
Vladimir Perevalov

Reputation: 4157

Some containers, like Unity have a function to provide dependencies to already existing instances. Of course this will not work for construcor injection. But it will work find for propery/method injection.

You can create your WPF Page (or whatever control), it will load markup, and then call Unity.BuildUp(your_object).

Or equivalent function of different container.

Upvotes: 0

Remo Gloor
Remo Gloor

Reputation: 32725

Have a look at the various MVVM frameworks like Caliburn Micro: http://caliburnmicro.codeplex.com/ Most of them come with IoC container support.

Upvotes: 1

Related Questions