User control Dependency Injection

Topics: Prism v2 - Silverlight 2
May 29, 2009 at 4:23 PM

I have a user control that I am using in a  databinding scenario.  The control is being bound inside a datagrid.   This control has a viewModel and I when I try to implement the following construct

public MediaBuyControl( IMediaBuyViewModel viewModel)

I am gettign an AG_Parser_Error.

In my module, I have the view model registered as I do other viewModel.

            container.RegisterType<IMediaBuyViewModel, MediaBuyViewModel>();

The control I am binding to is not currently registered with the regionManager because it is not intended to be a view.  Is it possible to use the Unity Container in this manner when databinding a control?

May 29, 2009 at 7:43 PM

Hi djnelson,


Some time ago there was a discussion about a similar issue that might help in your scenario:

·         Xaml and Dependency Injection   


So, as that discussion explains, you won't be able instantiate an object through XAML using a constructor with parameters.


On the other hand, as you said you don't necessarily need to register an object in the container for it to be injected. If you want properties and other injection performed to an instance when you do not control its construction, you can use the BuildUp method of the Unity container. This will make the object go through the container strategies that can be executed after object creation, such as property or method injection. This approach might help you inject some dependencies in your control AFTER it has been initialized.


For more information about BuildUp method you can check:

·         Using BuildUp to Wire Up Objects Not Created by the Container


Hope it helps!


Matias Bonaventura

May 29, 2009 at 8:36 PM

Hi Matias,

Thanks for the reply.  I looked at the buildup examples and they show the code starting with myContainer.  Thats turning out to be the rub.  I can create a new instance of the UnityContainer. but I really need to get to the instance of the container that was created Bootstrapper.  

So maybe this is a better way to phrase my problem.  I have a control that I am binding in datagrid.  I need to wire this control to the event aggregator to subscribe to some events.  As from the earlier discussion, I can not use the dependency injection as the XAML needs an empty constructor.  So from inside this constructor, how would it be possible to reference to the EventAggregator?

OK, the lights came on I have a most simple solution for this.  In the bootstrapper, I add the Container object and the EventAggregator object to the Application.Current.Resources and then in the constructor of my bound control I now have the references.  While I really like DI, it did put on the blinders as this solution did not occur to me sooner.

Take care and thanks again.

May 31, 2009 at 12:05 PM
Edited May 31, 2009 at 12:07 PM

Hi djnelson!


Sorry to say, but maybe these kind of errors can be a design issue in the application, but can't tell this from the original mail.

So to focus on the solution I'd consider using ServiceLocator in your project. ( that solves your problem, since you can access your container thru the ServiceLocator.Current and it can be initialized in the bootstrapper.


We're using it in different scenarios, this way we don't have to pass around container instances in some rare situations where the container really needed. (For example external components).




Jun 1, 2009 at 2:21 AM

Thanks Attila,

I reviewed the service locator and I can see where this would be useful, yet digging into it a bit, I can not really see what the difference from using Application.Current.Resources() for a reference for both the UnityContainer and the the EventAggregator.  The dictionary objects in this storage location can only be set once.

It is very common to want to bind user controls in a DataGrid or an ItemsCollection.  These items are required to have a parameterless constructor.  So, my challenge was how to get the EventAggregator to this control (more correctly, it's ViewModel to listen for StateChanges which are external ( Duplexing (Push) in my case).  By simply setting the Applcation.Current.Resource in the bootstrapper, I could now subscribe to the needed events in the constructor for the bound control.

I think both the UnityContainer and the EventAggregator are first class application resources and this is the simplest thing I could do solve my problem.