T4mvc: Can't inherit a controller class that doesn't have a default constructor?

I am using T4MVC with MVC2.

I have the following building blocks:

  • A simple object interface that defines that each POCO object must have a long Id property:

     public interface IEntity { public long Id; } 
  • A simple POCO class that implements the IEntity interface and has some string properties:

     public class CD : IEntity { public long Id { get; set; } public long Name { get; set; } } 
  • 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 (where CDManager implements the IEntityManager interface, which is the UnitOfWork template for adding 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 at compile time:

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

How can i solve this?

+8
constructor controller t4mvc
source share
3 answers

I wanted the controller base class to be abstract, and the constructor to be protected and parameterized. Get this problem by adding an empty constructor to the ControllerBase that throws a NotImplementedException.

Not quite right, but he is doing his job. The only problem is that, in conjunction with dependency injection, the wrong constructor will be called - since it throws an exception, the application will crash.

the 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 create compiled code. It seems that the error is always trying to generate an empty constructor (without parameters) for controller classes.

Hope this helps someone.

+7
source share

I see the problem, and it boils down to the fact that T4MVC is not entirely correct when dealing with generic classes. Usually it generates a default ctor for it in a partial class, but the fact that it generates it discards.

You should be able to work simply by adding the default ctor yourself, for example.

 public abstract partial class EntityController<T> : Controller where T : class, IEntity { public EntityController() { } // etc... } 
+2
source share

I noticed something very strange:

I added an empty constructor to the base class, but without throw new NotImplementedException(); and it works great.

But here's the weird thing when you call the controller, if I have the url / {controller}? params (the default value is set to Index in RouteConfig) the taskless closed controller is called in the base class. But when do I have a url like / {controller} / {action}? Params, the constructor with parameters is called.

0
source share

All Articles