Passing IUnityContainer to child

Topics: Prism v2 - WPF 3.5
Jun 23, 2009 at 4:49 PM

I have a working Prism V2 app. I am working on a module that has hundreds of visual elements, so I have divided them out into user controls ( within the Module project).

Now I am trying to bind data to them, which is provided from a service that is registered with the IUnityContainer.

How do I pass the container on to the user controls ? I tried adding it to the the constructor, but that causes an error that WPF wants a parameterless constructor.

Alternately, can I pass a resource defined in the main window on to the child?

Thanks.

Larry

Jun 23, 2009 at 8:03 PM
Edited Jun 23, 2009 at 10:22 PM

Hi Larry,

 

If I understood you scenario correctly, you want to create a UserControl that binds to data provided by a service. If that is the case, there are 3 approaches that I can think of that might be suitable in your scenario:

 

1.      Instantiate the UserControl with the container

You can achieve this using the Resolve method of the UnityContainer:

YourUserControl yourControl = unityContainer.Resolve<YourControl>();

This will allow you to inject dependencies though the User control constructor (this is how views are injected). As you said, the drawback of this approach is that you won't be able to declare the usercontrol in XAML.

 

2.      Create DependencyProperties in the control and bind to them from the view

When you define a UserControl you can define DependencyProperties. DependencyProperties are like .Net usual properties, but support databinding, animation, etc. You can read further here: Control Authoring Overview.

So for example, if your service provides a customer which your control needs to bind to, you can create a Customer DependencyProperty in the control and bind that property to the customer returned by the service.

 

3.      Create a IUnityContainer property in the control

If you define a public IUnityContainer property in the userControl, you can then set it from the view.  This might not be a good idea if you are planning to reuse the control in other scenarios, as you would be coupling the control to the container. It would be better to make the control depend on the service, or if you want a fully reusable control use the 2nd approach.

 

Hope it helps!

 

Matias Bonaventura

http://blogs.southworks.net/matiasb

Jun 23, 2009 at 8:31 PM

Thanks Matias!

Can you show an example of the binding for the IUnityContainer ( #3 ) ?

In #1, I presume that my module needs to register each of the controls,correct ?

Jun 23, 2009 at 10:22 PM

Hi Larry,

 

Sure, here is a possible code for #3. Please note that this should be adapted to the presentation pattern you are using:

In the View XAML (instanciate the UserControl):

<Controls:MyControl x:Name="MyControl"></Controls:MyControl>

 

In the View Codebehind / presenter (set the value to the property):

this.MyControl.Container = container;

NOTE: this need to be done before the InitializeComponent method from the view was called

 

In the UserControl Codebehind (declare the property):

public partial class MyControl : UserControl

{

    public IUnityContainer Container { get; set; }

 

    public MyControl()

    {

        InitializeComponent();

    }       

}

NOTE: because of the Initialization flow, the Container property will be null during the UserControl constructor.

 

Regarding your question about #1:  no, Unity can resolve concrete classes without registration, so you won't need to register controls in #1. You can read futher about Unity registration in:

·         Unity Application Block Methods

 

Hope it helps!

 

Matias Bonaventura

http://blogs.southworks.net/matiasb