container with Unity and MEF

Topics: Prism v4 - Silverlight 4
Jul 20, 2011 at 4:42 PM

Hey guys,
In some examples we can see code like this:

.......
// View discovery
this.regionManager.RegisterViewWithRegion("MainRegion", () => this.container.Resolve<EmployeeView>());
.....

Pls correct me if I am wrong.
1. This code this.container.Resolve<EmployeeView>() resolves EmployeeView using unity.
2. container is resolved most likely in class constructor like this SomeClassConstructor(IUnityContainer unityContainer){contrainer = unityContainer}

Now If I use MEF I should
1. use eihter [ImportingConstructor] or [ImportAttribute] public EmployeeView emplView{get;set;} and later
2. regionManager.RegisterViewWithRegion("MainRegion", () => emplView);

In Mef style we don't use container to resolve objects (well of cource we use, but not directly like in unity), right?

I am sort of confused with Unity/MEF

Jul 20, 2011 at 9:21 PM
Edited Jul 20, 2011 at 9:54 PM

http://blogs.msdn.com/b/bobbrum/archive/2010/11/22/prism-4-in-silverlight-with-mef-compositioninitializer.aspx

http://mef.codeplex.com/wikipage?title=CompsitionInitializer&version=2

http://mef.codeplex.com/wikipage?title=Guide&referringTitle=Documentation

http://forums.silverlight.net/p/215491/510782.aspx

http://pontusmunck.com/2010/04/07/using-mef-as-an-ioc-container/

http://www.wintellect.com/CS/blogs/jlikness/archive/2010/02/26/mef-instead-of-prism-for-silverlight-3-part-1-of-2-dynamic-module-loading.aspx

Jul 20, 2011 at 10:01 PM

Mef can be used with a service locator just like unity....

Mef and unity have their own IOC containers...Mef's container is  composition initializer

THis is is just my opinion...I like unity's container system....better than mef

If  are using service locator....Mef...I suspect...is better    

Jul 20, 2011 at 10:13 PM
Edited Jul 20, 2011 at 10:27 PM

http://blogsprajeesh.blogspot.com/search/label/MEF

http://csharperimage.jeremylikness.com/2010/03/viewmodel-binding-with-managed.html-   service locator pattern 

http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/08/04/experimenting-with-prism-4-silverlight-mef.aspx 

here mike taulty talks about difference between mef and unity using their own ioc containers

 

Developer
Jul 21, 2011 at 1:56 PM

Hi,

To add to what the user billism is posting:

What you are saying seems to be correct, when using MEF it’s not a common practice to resolve objects using the container. Instead, the objects are commonly injected using the [ImportingConstructor] or [Import]  attributes as you have mentioned.

However, it is possible to resolve the objects using the CompositionContainer’s GetExportedValue method. For example, in the Bootstrapper of the Modularity for Silverlight with MEF QuickStart this container is used to obtain an instance of the Shell, as shown in the code below:

 

protected override DependencyObject CreateShell()

{

    return this.Container.GetExportedValue<Shell>();

}

 

Note that you have to export the CompositionContainer in order to use it from another component (such as a module) and, as mentioned above, this is not a common practice.

Another approach for this case could be using an overload of the RegistryViewWithRegion method which accepts a type instead of an instance:

this.regionManager.RegisterViewWithRegion("MainRegion", typeof(EmployedView));

Or to use the ServiceLocator to resolve the view:

this.regionManager.RegisterViewWithRegion("MainRegion", () => ServiceLocator.Current.GetInstance<EmployedView>() );

Note that these two approaches can be applied regardless of whether you are using MEF or Unity (as the service locator abstracts away of the container that is being used, and using the RegisterViewWithRegion method passing the type of the view will internally use the service locator to resolve an instance of it).

I hope you find this useful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jul 21, 2011 at 4:18 PM
billsim wrote:

http://blogs.msdn.com/b/bobbrum/archive/2010/11/22/prism-4-in-silverlight-with-mef-compositioninitializer.aspx

Question related to article above:

If instead of initializing CompositionHost with MEF  container as it shown in the article
for sake of experiment I initializes CompositioniHost with deployment catalog like this:

var deploymentCatalog = new DeploymentCatalog(); // should add all parts in current xap.
CompositionHost.Initialize(deploymentCatalog)

This should be similar to sharing container with prism.
protected override CompositionContainer CreateContainer()  
 { var container = base.CreateContainer();
   CompositionHost.Initialize(container);       
    return container;   
}

Now in classes initialized without help of Prism I can resolve almost all classes in Prism framework with CompositionInitializer. But If i thy to resolve ILoggerFacade  - exception..

Question is why code
var deploymentCatalog = new DeploymentCatalog();
CompositionHost.Initialize(deploymentCatalog)
doesn't see ILoggerFacade -  Microsoft.Practices.Prism assembly is in my XAP and it means all classes with [Export] should be available. Or ILoggerFacade doesn't have [ExportAttribute]? Or I missed something?

Jul 21, 2011 at 4:36 PM
Edited Jul 21, 2011 at 4:40 PM
Pls disregard this question.
Prism doesn't export ILoggerFacade, but creates it in Run method and after registers in Catalog, thats why I don't see it in  DeploymentCatalog()

 

vit100 wrote:

billsim wrote:

http://blogs.msdn.com/b/bobbrum/archive/2010/11/22/prism-4-in-silverlight-with-mef-compositioninitializer.aspx

Question related to article above:

If instead of initializing CompositionHost with MEF  container as it shown in the article
for sake of experiment I initializes CompositioniHost with deployment catalog like this:

var deploymentCatalog = new DeploymentCatalog(); // should add all parts in current xap.
CompositionHost.Initialize(deploymentCatalog)

This should be similar to sharing container with prism.
protected override CompositionContainer CreateContainer()  
 { var container = base.CreateContainer();
   CompositionHost.Initialize(container);       
    return container;   
}

Now in classes initialized without help of Prism I can resolve almost all classes in Prism framework with CompositionInitializer. But If i thy to resolve ILoggerFacade  - exception..

Question is why code
var deploymentCatalog = new DeploymentCatalog();
CompositionHost.Initialize(deploymentCatalog)
doesn't see ILoggerFacade -  Microsoft.Practices.Prism assembly is in my XAP and it means all classes with [Export] should be available. Or ILoggerFacade doesn't have [ExportAttribute]? Or I missed something?