t4mvc:不能继承一个没有默认构造函数的控制器类? [英] t4mvc : Cannot inherit a controller class which has no default constructor?

查看:298
本文介绍了t4mvc:不能继承一个没有默认构造函数的控制器类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用T4MVC与MVC2。

I am using T4MVC with MVC2.

我有以下构建块:


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

public interface IEntity
{
    public long Id;
}


  • 一个简单的POCO类, IEntity 接口并具有一些字符串属性:

  • 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);
    }
    


  • CDController (其中 CDManager 实现 IEntityManager 接口,这是一个UnitOfWork模式添加CRUD功能):

  • 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()) { }
    }
    


  • 当我运行t4模板时,会生成以下代码:

    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>不包含带有0个参数的构造函数

    MyApp.EntityController<CD> does not contain a constructor that takes 0 arguments

    我如何解决这个问题?

    推荐答案

    我想要的控制器基类是抽象的,它的构造函数保护和参数化。通过向ControllerBase添加一个空的构造函数来抛出NotImplementedException,来解决这个问题。

    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.

    代码:

    public abstract class ControllerBase : Controller
    {
        protected object AlwaysSupply { get; private set; }
    
        public ControllerBase()
        {
            throw new NotImplementedException();
        }
    
        public ControllerBase(object alwaysSupply)
        {
            AlwaysSupply = alwaysSupply;
        }
    }
    

    这将导致T4MVC生成可编译代码。该错误似乎总是尝试为控制器类生成一个空白(无参数)构造函数。

    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.

    希望这有助于某人。

    这篇关于t4mvc:不能继承一个没有默认构造函数的控制器类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆