views:

45

answers:

2

I've used MEF for a few projects at work and I've just stared messing about with Monorail in my spare time. I was wondering if there was any way that I could use MEF to load the controllers that Monorail uses. Monorail appears to look for controllers in assemblies that you list in the Web.Config:

<controllers>
  <assembly>my.assembly</assembly>
</controllers>

Is there a way that 'my.assembly' can then use MEF to load up more controllers? I have to admit I haven't though of a reason I would need this functionality but I'm just trying things out!

+1  A: 

It's not something you can use yet, but have a look at Hammett's blog post here, where he talks about what he is prototyping for MonoRail 3.0 (including support for MEF by default).

Daniel Plaisted
Looks promising. Could be a while until released/stable though
Tim
+1  A: 

Monorail is a very extensible framework. Almost everything is being provided by a service that can be easily switched with something else. For e.g., IoC integration is quite easy, as you can switch the services that creates controllers, filters, helpers, and all other MonoRail entities.

Concrete example: Integrating Windsor container into Monorail

Now this sets up almost everything in the Monorail to be provided by Windsor. If you only want Controllers to be provided by MEF, there's even less work.

I have very little working knowledge of MEF so it might be a little off, but you'd get the general idea:

  1. Use MEF discovery mechanisms to locate controller types, then add controller types to the default IControllerTree service. take a peek at MonoRailFacility.cs for inspiration.
  2. Implement a MefControllerFactory : IControllerFactory that will use MEF to instantiate controllers when needed. Inspiration is at WindsorControllerFactory.cs
Ken Egozi
This page (http://blogs.msdn.com/b/hammett/archive/2009/04/23/mef-and-asp-net-mvc-sample.aspx) has helped me for IControllerFactory. But MonoRailFacility appears to access the default IControllerTree through Windsor. How should I be accessing it?
Tim
I believe that you need to access a Mef Catalog instead of Windsor's implementation of IControllerTree, for supplying you with controllers.Hammet's example has a MecControllerCatalog for reference.I do not have much Mef experience, so the details of that parts won't come from me, sorry :)
Ken Egozi
Hammet's MefControllerFactory does the MEF loading. But MR only looks in the default IControllerTree service for controllers. Looks like I could either write my own IControllerTree service, or add controllers to the existing one. Looks like http://hammett.castleproject.org/?p=159 and section "Adding controllers and views in runtime" is pointing me in the right direction. If my MefControllerFactory overrides Service(System.IServiceProvider service) then I can just: IControllerTree tree = service.GetService(typeof(IControllerTree)); tree.AddController("area", "Home", typeof(MyCoolController));
Tim
MefControllerFactory would obviously have to inherit AbstractControllerFactory...
Tim