Using Logger in Prism

Topics: Prism v4 - WPF 4
Feb 11, 2011 at 5:06 AM

Hi,

<Silly question ahead/>

A basic conceptual doubt, I am registering a Custom facade object as part of my container (using MEF) in bootstrapper class. Now I can query this object from Container using another class (say Class Consumer) is by "container.GetExport< CustomFacade >()". For using "container" object, I need to create a CompostionContainer object as part of my Consumer class and use it.

Is it the right approach creating Container object everytime we need to query something from it? Or better approach is to create a static instance of Container and use it through out the app?

Thanks!

regards,

Indro

Feb 11, 2011 at 6:08 AM
Edited Feb 11, 2011 at 6:33 AM

Niether, just run the BootStrapper and either use Imports or use the Service Locator.

Reference the Microsoft.Practices.ServiceLocation dll then:

using Microsoft.Practices.ServiceLocation;

ServiceLocator.Current.GetInstance<CustomFacade>();

Took me a while to find ServiceLocator, when I started, so don't feel bad that you missed it, doesn't seem to be too clear in the docs.  The container you created in the BootStrapper does persist, you don't need to create another one.  In most of the samples, using Imports is more common, but ServiceLocator comes in handy.

Feb 11, 2011 at 6:27 AM

John,

GetExport also does the same. It will get me a instance of the CustomFacade object.

Is there a way out using GetExport and container (MEF way)?

 

Thanks,

Indro

Feb 11, 2011 at 6:28 AM

John,

GetExport also does the same. It will get me a instance of the CustomFacade object.

Is there a way out using GetExport and container (MEF way)?

 

Thanks,

Indro

Feb 11, 2011 at 3:02 PM

Hi Indro,

As John told you in his post, it will be more simple if you simple decorate your dependencies with the Import attribute on your classes or also, you could obtain the instance using the service locator.

Prism provides guidance on how to resolve instances using a Dependency Injection Container. In your particular scenario, it is using MEF. You will find this topic explained in Chapter 3: Managing Dependencies Between Components.

Additionally, you read more about MEF and how it works here.

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Feb 11, 2011 at 6:15 PM
Edited Feb 11, 2011 at 9:52 PM
Indrasis wrote:

GetExport also does the same. It will get me a instance of the CustomFacade object.

Yes, that was kind of the point.  But, you don't have direct access to the container that is created in the bootstrapper, once you leave the bootstrapper.  You do have access to ServiceLocator.Current, which does have access to that container.


Feb 14, 2011 at 9:29 AM

John and Miguel,

Thanks for the clarification.

John's solution is perfect. I decorate my ServiceClass with [Export] attribute and add the module to AggregateCatalog as MEF guidance says. Then I use,

var x= ServiceLocator.Current.GetInstance<ServiceClass>(), which returns the instance! Perfect.

Just to get a piece of your thought process,  Is there a way to get a handle to the CompositionContainer of Bootstrapper class from any other module?

Thanks,

Indro