Your equipment here is quite solid and will work even with partial trust. In the nerd dinner MEF example, there are extensions that allow you to deal with the detection of controllers by convention and automatically turn them into MEF exports without putting them using MEF. But managing parts catalogs does not directly work in partial trust, so MEF methods for a botanical dinner do not work in partial trust.
If you work with complete trust and want, based on agreement with your controllers, to start with the MER Nerd Dinner example, but you probably should also read about a few serious issues with the MEF Nerd lunch example that will appear if your own application model located in a separate class of the library project. I wrote about these cases and suggested some corrections.
If you are not all interested in Congress-based materials, then an example of courting a curtain is a bit more complicated. Your decision is probably as beautiful as it is ... and also works in partial trust, which is always a bonus.
[update] I noticed one potential problem with your technique:
var controllerExport = controllers.Where(x => x.Value.GetType() == controllerType).FirstOrDefault();
In the where clause here, you call .Value for each export in the parts collection ..., which will actually cause each of these exported instances to be created and created for evaluation. This can be a nasty performance issue.
You might consider decorating your controllers with named export contracts as follows:
[Export("Home", typeof(IController))]
Then, using this factory control instead:
public class MefControllerFactory: IControllerFactory { private CompositionContainer _Container; public MefControllerFactory(Assembly assembly) { _Container = new CompositionContainer(new AssemblyCatalog(assembly)); } #region IControllerFactory Members public IController CreateController(RequestContext requestContext, string controllerName) { var controller = _Container.GetExportedValue<IController>(controllerName); if (controller == null) { throw new HttpException(404, "Not found"); } return controller; } public void ReleaseController(IController controller) { // nothing to do } #endregion }
Stephen M. Redd
source share