t4mvc:无法继承没有默认构造函数的控制器类?

发布于 2024-11-24 21:38:40 字数 1131 浏览 3 评论 0原文

我将 T4MVC 与 MVC2 一起使用。

我有以下构建块:

  1. 一个简单的实体接口,定义每个 POCO 实体必须具有一个long Id 属性:

    公共接口IEntity
    {
        公共长ID;
    }
    
  2. 一个实现 IEntity 的简单 POCO 类接口并具有一些字符串属性:

    公共类CD:IEntity
    {
        公共长ID { 获取;放; }
    
        公共长名称{获取;放; }
    }
    
  3. 基本控制器:

    公共抽象类EntityController; :控制器,其中 T :类,全局::IEntity
    {
        公共 EntityController(IEntityManager 经理);
    }
    
  4. 我在我的 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:

  1. A simple entity interface which defines that every POCO entity must have a long Id property:

    public interface IEntity
    {
        public long Id;
    }
    
  2. 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; }
    }
    
  3. A base controller:

    public abstract class EntityController<T> : Controller where T : class, global::IEntity
    {
        public EntityController(IEntityManager<T> manager);
    }
    
  4. I use this base controller in my CDController (where CDManager implements the IEntityManager 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱咩 2024-12-01 21:38:40

我希望控制器基类是抽象的,并且它的构造函数受到保护和参数化。通过向 ControllerBase 添加一个抛出 NotImplementedException 的空白构造函数来解决此问题。

感觉不太对劲,但它完成了工作。唯一的问题是,当与依赖注入结合使用时,将调用错误的构造函数 - 因为它会抛出异常,应用程序将崩溃。

代码:

public abstract class ControllerBase : Controller
{
    protected object AlwaysSupply { get; private set; }

    public ControllerBase()
    {
        throw new NotImplementedException();
    }

    public ControllerBase(object alwaysSupply)
    {
        AlwaysSupply = alwaysSupply;
    }
}

这将导致 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:

public abstract class ControllerBase : Controller
{
    protected object AlwaysSupply { get; private set; }

    public ControllerBase()
    {
        throw new NotImplementedException();
    }

    public ControllerBase(object alwaysSupply)
    {
        AlwaysSupply = alwaysSupply;
    }
}

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.

送舟行 2024-12-01 21:38:40

我看到了这个问题,它归结为 T4MVC 在处理泛型类时没有做正确的事情。通常它会在分部类中为其生成一个默认构造函数,但事实上它是通用的,因此无法实现。

您应该能够通过自己添加默认 ctor 来解决此问题,例如

public abstract partial class EntityController<T> : Controller where T : class, IEntity {
    public EntityController() { }

    // etc...
}

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.

public abstract partial class EntityController<T> : Controller where T : class, IEntity {
    public EntityController() { }

    // etc...
}
谁的新欢旧爱 2024-12-01 21:38:40

我注意到一些非常奇怪的事情:

我已将空构造函数添加到基类中,但没有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文