t4mvc:无法继承没有默认构造函数的控制器类?
我将 T4MVC 与 MVC2 一起使用。
我有以下构建块:
一个简单的实体接口,定义每个 POCO 实体必须具有一个
long Id
属性:公共接口IEntity { 公共长ID; }
一个实现
IEntity
的简单 POCO 类接口并具有一些字符串属性:公共类CD:IEntity { 公共长ID { 获取;放; } 公共长名称{获取;放; } }
基本控制器:
公共抽象类EntityController
; :控制器,其中 T :类,全局::IEntity { 公共 EntityController(IEntityManager 经理); } 我在我的
CDController
中使用这个基本控制器(其中CDManager
实现了IEntityManager
接口,这是一个添加 CRUD 功能的 UnitOfWork 模式):公共分部类 CDController : EntityController
; { 公共 CDController() : 基(new CDManager()) { } }
当我运行 t4 模板时,会生成以下代码:
namespace MyApp.Web.Controllers {
public partial class CDController {
[GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
protected CDController(Dummy d) { }
但这在编译过程中给了我一个错误:
MyApp.EntityController
;不包含带有 0 个参数的构造函数
我该如何解决这个问题?
I am using T4MVC with MVC2.
I have the following building blocks:
A simple entity interface which defines that every POCO entity must have a
long Id
property:public interface IEntity { public long Id; }
A simple POCO class which implements the
IEntity
interface and has some string properties:public class CD : IEntity { public long Id { get; set; } public long Name { get; set; } }
A base controller:
public abstract class EntityController<T> : Controller where T : class, global::IEntity { public EntityController(IEntityManager<T> manager); }
I use this base controller in my
CDController
(whereCDManager
implements theIEntityManager
interface, which is a UnitOfWork pattern to add CRUD functionality):public partial class CDController : EntityController<CD> { public CDController() : base(new CDManager()) { } }
When I run my t4 template, this code is generated:
namespace MyApp.Web.Controllers {
public partial class CDController {
[GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
protected CDController(Dummy d) { }
But this gives me an error during compilation:
MyApp.EntityController<CD> does not contain a constructor that takes 0 arguments
How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我希望控制器基类是抽象的,并且它的构造函数受到保护和参数化。通过向 ControllerBase 添加一个抛出 NotImplementedException 的空白构造函数来解决此问题。
感觉不太对劲,但它完成了工作。唯一的问题是,当与依赖注入结合使用时,将调用错误的构造函数 - 因为它会抛出异常,应用程序将崩溃。
代码:
这将导致 T4MVC 生成可编译的代码。错误似乎是它总是尝试为控制器类生成一个空白(无参数)构造函数。
希望这对某人有帮助。
I wanted by controller base class to be abstract and it's constructor protected and parametrized. Got around this issue by adding a blank constructor to ControllerBase that throws a NotImplementedException.
Doesn't quite feel right but it gets the job done. Only issue is when combined with dependency injection the wrong constructor will be called - since it throws an exception the app will bum out.
Code:
This will cause T4MVC to generate compilable code. The fault seems to be it always tries to generate a blank (no parameters) constructor for controller classes.
Hope this helps someone.
我看到了这个问题,它归结为 T4MVC 在处理泛型类时没有做正确的事情。通常它会在分部类中为其生成一个默认构造函数,但事实上它是通用的,因此无法实现。
您应该能够通过自己添加默认 ctor 来解决此问题,例如
I see the problem, and it comes down to T4MVC not quite doing the right thing when dealing with generic classes. Normally it would generate a default ctor for it in a partial class, but the fact that it's generic is throwing it off.
You should be able to work around simply by adding a default ctor yourself, e.g.
我注意到一些非常奇怪的事情:
我已将空构造函数添加到基类中,但没有
throw new NotImplementedException();
并且它工作正常。但这是奇怪的事情,当我调用控制器时,如果我有一个像这样的网址
/{controller}?params (默认操作设置为 RouteConfig 中的 Index)基类上的无参数私有控制器被调用。
但是,当我有一个像 /{controller}/{action}?params 这样的 url 时,就会调用带参数的构造函数。
I've noticed something very odd:
I've added the empty constructor to the base class, but without the
throw new NotImplementedException();
and it works fine.But here's the odd thing, when calling the controller if I have an url like
/{controller}?params (default action being set to Index in the RouteConfig) the parameterless private controller on the base class is called.
But when I have an url like /{controller}/{action}?params then the constructor with parameters is called.