ASP.NET MVC Generic Controllers and Spring.NET

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2010-05-20T15:31:44Z Indexed on 2010/05/21 18:00 UTC
Read the original article Hit count: 249

Filed under:
|

Hello,

I am creating an application using ASP.NET MVC (2) and Spring.NET.

Since most of my Controller implementations just implement the similar CRUD operations, I would like to just create a single Generic controller, as explained here:

http://stackoverflow.com/questions/848904/in-asp-net-mvc-is-it-possible-to-make-a-generic-controller

However, the above example doesn't take DI frameworks into consideration.

What I'm thinking is to create this (warning: this is an ugly mass of code I need help with):

public SpringGenericControllerFactory : DefaultControllerFactory {

    public IController CreateController(RequestContext requestContext, string controllerName) {

        // Determine the controller type to return
        Type controllerType = Type.GetType("MyController").MakeGenericType(Type.GetType(controllerName));

        // Return the controller
        return Activator.CreateInstance(controllerType) as IController;

    }
}

The entries in objects.xml would look something like this:

<object id="controllerFactory" type="Application.Controllers.SpringGenericControllerFactory" />

<object id="DepartmentController" factory-method="CreateController" factory-object="controllerFactory" />

Can anyone pick through this and offer advice?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about spring.net