Spring.net Controller Factory


The Spring.net Controller Factory implementation, MVCContrib.ControllerFactories.SpringControllerFactory, for MVC Contrib allows the configuration of IController implementations within the Spring.net DI container.

Custom Controller Factories need to be configured in order to be recognized by the MVC framework. This configuration will need to take place within the Application_Start() method of the Globabl.asax.cs code file in your MVC Application. In order for the Controller Factory to share the Spring.net context with other components in the application, developers should create the context before setting the MVC framework default controller factory. Passing the context to the static Configure() method will allow this sharing to occur. Either IApplicationContext or IObjectFactory Spring.net instances are supported.

The names for each IController instance in the Spring.net configuration should match the Type.Name of each concrete controller implementation that is created.

Note: The version included with MVCContrib was developed and tested against version 1.1 of Spring.net. However, no functionality specific to 1.1 was used and there is no reason why the class could not be compiled against a prior version of Spring.net, if that is requirement for usage.

Example Usage

The following is an example of the additions that need to be made in Global.asax.cs:

protected void Application_Start(object sender, EventArgs e)
    //This example just sets the default context for use with the Spring.net Controller
    //Factory.  An IObjectFactory instance is also supported, if greater control over
    //Spring.net is desired.

    IApplicationContext ctx = ContextRegistry.GetContext();


    //more configuration code for the MVC framework

Below is a sample IController implementation that will be configured in Spring.net:

public class SimpleController : IController
    public void Execute(ControllerContext controllerContext)
        //controller code goes here

The configuration of this controller in Spring.net looks like this:

<objects xmlns="http://www.springframework.net"
                xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">

  <object id="SimpleController" singleton="true" type="Examples.SimpleController"/>


Last edited Dec 14, 2007 at 6:12 PM by darrenstokes, version 3


rafal_g Nov 25, 2008 at 8:25 AM 
This example is broken - controller cannot be configured as singleton! It must be a prototype (use singleton=false).
Otherwise application will not work correctly.