Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Azure, PHP, OpenXML, VSTS, ...

About the author

Maarten Balliauw is currently employed as .NET Technical Consultant at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP and the Windows Azure cloud platform.
More about me More about me
Send mail E-mail me


ASP.NET MVC Quickly Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn
View Maarten Balliauw's MVP profile

Search

Latest Twitter

    Follow me on Twitter...

    My projects

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2010

    ASP.NET MVC Chained Controller Factory

    My last post on the REST for ASP.NET MVC SDK received an interesting comment… Basically, the spirit of the comment was: “There are tons of controller factories out there, but you can only use one at a time!”. This is true. One can have an IControllerFactory for MEF, for Castle Windsor, a custom one that creates a controller based on the current weather, … Most of the time, these IControllerFactory  implementations do not glue together… Unless you chain them!

    kick it on DotNetKicks.com

    Chaining IControllerFactory

    The ChainedControllerFactory that I will be creating is quite easy: it builds a list of IControllerFactory instances that may be able to create an IController and asks them one by one to create it. The one that can create it, will be the one that delivers the controller. In code:

    public class ChainedControllerFactory : IControllerFactory
    {
        const string CHAINEDCONTROLLERFACTORY = "__chainedControllerFactory";

        protected List<IControllerFactory> factories = new List<IControllerFactory>();

        public ChainedControllerFactory Register(IControllerFactory factory)
        {
            factories.Add(factory);
            return this;
        }

        public IController CreateController(RequestContext requestContext, string controllerName)
        {
            IController controller = null;
            foreach (IControllerFactory factory in factories)
            {
                controller = factory.CreateController(requestContext, controllerName);
                if (controller != null)
                {
                    requestContext.HttpContext.Items[CHAINEDCONTROLLERFACTORY] = factory;
                    return controller;
                }
            }

            return null;
        }

        public void ReleaseController(IController controller)
        {
            IControllerFactory factory =
                HttpContext.Current.Items[CHAINEDCONTROLLERFACTORY] as IControllerFactory;
            if (factory != null)
                factory.ReleaseController(controller);
        }
    }

    We have to register this one as the default IControllerFactory in Global.asax.cs:

    protected void Application_Start()
    {
        ChainedControllerFactory controllerFactory = new ChainedControllerFactory();
        controllerFactory
            .Register(new DummyControllerFactory())
            .Register(new OnlyHomeControllerFactory())
            .Register(new DefaultControllerFactory());

        ControllerBuilder.Current.SetControllerFactory(controllerFactory);

        RegisterRoutes(RouteTable.Routes);
    }

    Note: the DummyControllerFactory and the OnlyHomeControllerFactory are some sample, stupid IControllerFactory implementations.

    Caveats

    There is actually one caveat to know when using this ChainedControllerFactory: not all controller factories out there follow the convention of returning null when they can not create a controller. The ChainedControllerFactory expects null to determine if it should try the next IControllerFactory in the chain.

    Download source code

    You can download example source code here: MvcChainedControllerFactory.zip (244.37 kb) (sample uses MVC 2, code should work on MVC 1 as well)

    kick it on DotNetKicks.com


    Comments

    DotNetKicks.com | Reply

    Friday, August 21, 2009 9:25 AM

    trackback

    ASP.NET MVC Chained Controller Factory

    You've been kicked (a good thing) - Trackback from DotNetKicks.com

    WebDevVote.com | Reply

    Friday, August 21, 2009 11:32 AM

    trackback

    ASP.NET MVC Chained Controller Factory

    You are voted (great) - Trackback from WebDevVote.com

    DotNetBurner - ASP.net MVC | Reply

    Friday, August 21, 2009 1:06 PM

    trackback

    ASP.NET MVC Chained Controller Factory

    DotNetBurner - burning hot .net content

    Troy Goode United States | Reply

    Friday, August 21, 2009 2:17 PM

    Troy Goode

    In other words, a ControllerFactoryFactory. Smile

    9eFish | Reply

    Friday, August 21, 2009 5:07 PM

    trackback

    ASP.NET MVC Chained Controller Factory

    9efish.感谢你的文章 - Trackback from 9eFish

    DotNetShoutout | Reply

    Friday, August 21, 2009 11:15 PM

    trackback

    ASP.NET MVC Chained Controller Factory - Maarten Balliauw

    Thank you for submitting this cool story - Trackback from DotNetShoutout

    blog.cwa.me.uk | Reply

    Monday, August 24, 2009 9:35 AM

    pingback

    Pingback from blog.cwa.me.uk

    Reflective Perspective - Chris Alcock  » The Morning Brew #418

    Robert United States | Reply

    Monday, October 26, 2009 10:19 PM

    Robert

    There is one more thing unclear to me: I am using Windsor controller factory in order to wire controllers with all required dependencies. Is there any possibility to continue using Windsor and yet to take advantage of  ResourceControllerFactory from REST for ASP.NET MVC SDK?

    maartenba Belgium | Reply

    Tuesday, October 27, 2009 7:57 AM

    maartenba

    The ResourceControllerFactory wraps around the existing controller factory, so if you register your Windsor factory first, followed by the ResourceControllerFactory, you should be fine.

    Add comment




      Country flag

    biuquote
    • Comment
    • Preview
    Loading