Search code examples
c#mefcastle-monorail

Can I load controllers in Monorail using MEF?


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!


Solution

  • 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