views:

20

answers:

2

I have an ASP.NET MVC2 application that supports visualization plug-ins/providers. The IVisualization interface is defined in a common assembly which is referenced by both the ASP.NET MVC2 app, and any visualization providers.

In the Visualization controller, I have a method which returns all the applicable visualizations for a given set of data. In order to scan the available providers, I use the following code in the controller's ActionMethod.

var catalog = new DirectoryCatalog(HttpRuntime.BinDirectory); 
var container = new CompositionContainer(catalog);
var visualizations = container.GetExportedValues<IVisualization>();

However, I feel like if I have the following in the controller

[ImportMany]
public IEnumerable<IVisualization> Visualizations { get; set; }

then the import should happen automatically. What am I missing that prevents the automatic imports?

Also, is the code that I am currently using going to kill scaling of website?

Thanks, Erick

A: 

Hi Erick,

If you have a controller that declares that particular property import, you must programatically call one of MEF's methods to satisfy them.

Some options are:

container.GetExportedValues<MyController();
container.ComposeParts(controllerInstance);

among others.

I hope this illustrates my point.

Damian Schenkelman
It does, thanks. The examples that I was looking at missed that. Thanks!
Erick T
+1  A: 

In order for MEF to satisfy the imports, it needs to also be responsible for the instantiation of the controller. You can do that in MVC by using a custom controller factory. You can find a sample (maybe outdated) of this in my blog: http://blogs.msdn.com/b/hammett/archive/2009/07/15/mef-and-asp-net-mvc-sample-updated.aspx

hammett