Prism ServiceLocator and Lazy load import

Topics: Prism v4 - Silverlight 4, Prism v4 - WPF 4
Jul 14, 2011 at 5:42 PM
Edited Jul 14, 2011 at 5:59 PM

I am trying to use the Prism ServiceLocator but require lazy loading.  Normally I use option 1 below and every works.  However in my case class A  is not part of the import chain that starts with Shell (CompaseParts(Shell)).  Therefore I turned to the Prism ServiceLocator, but do not understand how to lazy load.  I do not know how to correct Option 2 below to have the correct Contract (IContract) and Lazily load.

If absolutely necessary there may be an option3, which involves importing the container using ServiceLocator within the encompassing Class B.  Then calling container.ComposeExportedValue<IContract>(new Class A) which will resolve the ImportMany of Option 1.  I believe I read in the Prism forum that is best practice not to pass around the container as is defeats the ServiceLocator abstraction intent.

Class A
// Option 1: Works (Contract=IContract)
IEnumerable<Lazy<IContract, IContractMetaData>> operations;
// Option 2: Does not work (operations=null) because contract is wrong (Contract=Lazy<IContract, IContractMetaData>)
IEnumerable<Lazy<IContract, IContractMetaData>> operations;
operations = MyServiceLocator.GetAllInstances<Lazy<IContract, IContractMetaData>>();


Jul 14, 2011 at 6:54 PM


Based on my understanding of the ServiceLocator mechanism, it currently does not support lazy loading. Therefore, you could extend the service locator implementation to provide support for lazy loading, or you could go ahead with option 3. Although it might not be the best practice, it could serve as a proper workaround for your scenario.

You might find the CommonServiceLocation forums useful, in case you want to go deeper into the ServiceLocator mechanisms.

I hope you find this helpful.

Guido Leandro Maliandi