ASP.NET MVC 通用控制器和 Spring.NET
我正在使用 ASP.NET MVC (2) 和 Spring.NET 创建一个应用程序。
由于我的大多数控制器实现仅实现类似的 CRUD 操作,因此我只想创建一个通用控制器,如下所述:
但是,上面的示例没有将 DI 框架纳入其中考虑。
我的想法是创建这个(警告:这是一堆丑陋的代码,我需要帮助):
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;
}
}
objects.xml 中的条目看起来像这样:
任何人都可以选择这并提供建议吗?
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(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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两件事:
controllerName 参数实际上将作为“Department”而不是“DepartmentController”出现。您要么需要处理这个问题,要么调整您的objects.xml 中的名称。controllerName
如果上下文不返回控制器,请确保回退到默认实现。在这种情况下,您应该回退到基类实现。
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.