开发者

ASP.NET MVC Generic Controllers and Spring.NET

开发者 https://www.devze.com 2022-12-30 20:17 出处:网络
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

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:

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(Request开发者_高级运维Context 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?


Two things:

  • The controllerName argument will actually come through as 'Department' not 'DepartmentController'. You either need to handle this or adjust the name in your objects.xml

  • Make sure you fall back to the default implementation if the context does not return a controller. In this instance you should fall back to the base classes implementation.

0

精彩评论

暂无评论...
验证码 换一张
取 消