views:

108

answers:

5

What is the best way to transparently inject dependencies (using IOC container) to user controls in WPF?

I assume that user controls are part of XAML for the window or other user controls. Also I think parent (whoever it is) should not be responsible for this. Solution for manually injecting dependencies from parent looks not clean enough for me. I want to avoid managing the dependencies of my components explicitly as it voilates the idea of IOC.

Is any event which is raised when logical tree is being created so I can intercept it and inject my dependencies?

EDIT: by dependencies I also mean ViewModel, Controller, Presenter (whatever pattern is used)

Thanks, Andrey

+2  A: 

The best way to deal with dependencies in WPF is by following the MVVM pattern.

In short, you don't inject dependencies directly into User Controls (View), but rather into their DataContext (ViewModel).

Mark Seemann
Sure, but ViewModel is just example of the dependency
So how would you inject ViewModel to user control?
Set the DataContext property.
Mark Seemann
From parent control? it means that parent should be able to create ViewModel for user control (either using access to container or abstract factory). Also I don't like the idea of parent taking care of children dependencies - this violate idea of IoC. Anyway this is actually how I do now, but looking for the better ways.
No, from the parent ViewModel. See here for more information: http://stackoverflow.com/questions/2400108/query-on-mvvm-design-pattern-on-wpf/2400198#2400198
Mark Seemann
ViewModel is an implementation detail of my view and ideally nobody should aware even about its existence (including ViewModel for the control parent). I don't want to worry about initializing control.ViewModel everytime I put control in XAML. Ideally...
It sounds like you are using MVP and not MVVM. You are perfectly entitled to do that, but that's just not my recommendation...
Mark Seemann
I'm not. Also I've never heard that MVVM somehow define the way how you should manager controls hierarchy. Would be interested to read about that.
A: 

FrameworkElement has an Initialized event, which you could hook up and inject the dependencies. You should test if it comes early enough for your scenario.

Timores
Looks like it happens while view ctor is executed (from InitializeComponent)._container.Resolve<MainView>()mainView.Initialized += mainView_Initialized; // never happensmainView.Show();
A: 

The way i did it is to have an overall application class which injects dependencies into your viewmodel class (assuming your using MVVM design pattern ?) - use a DI container like Unity. See the WPF Application Framework (http://waf.codeplex.com/) which contains samples of just such a scenario that you're describing.

TanvirK
Not sure I understand how you access user controls from the application. Anyway this is what I want to avoid - managing dependencies.
The injection of dependencies can be taken care of by a dependency container like Unity. I don't know your specific case, but it looks like you're not using data binding (to say nothing of MVVM) ? If so, i recommend you investigate these aspects further - a lot of the power of WPF is because of these techniques.
TanvirK
no, I'm using MVVM, but this does not matter. And the question is of cause about injecting dependencies using IOC container (again does not matter which one).
A: 

I struggled with this mind block too:

Also I think parent (whoever it is) should not be responsible for this.

Then who will? The point of IoC is that something else (parent, view model, something, ...) defines the dependencies.

Ray
A: 

One of the possible ways to solve the problem is to go with "ViewModel First" approach and using convention over configuration.

Build Your Own MVVM Framework by Rob Eisenberg provides more details on this