U4-2501 - Give Umbraco.Web.Models.RenderModel a parameterless constructor

Created by Jeroen Breuer 11 Jul 2013, 14:54:45 Updated by Jeroen Breuer 12 Jul 2013, 04:35:32

I'm creating some custom models which inherit from the RenderModel and I'm trying to pass that model as a generic in another method, but that gives the following exception: "'Umbraco.Extensions.Models.NewsOverviewModel' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method". The NewsOverviewModel is the model which inherits from the RenderModel. Is it possible to give it a parameterless constructor?

Comments

Jeroen Breuer 11 Jul 2013, 15:06:06

Currently there is a constructor which always needs an IPublishedContent, but I always pass in the CurrentPage in there so that could also happen in the parameterless constructor.


Jeroen Breuer 11 Jul 2013, 15:29:48

For now I fixed it with this code:

public NewsOverviewModel() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent) {

}

That way I still have a parameterless constructor I can use.


Shannon Deminick 11 Jul 2013, 23:14:49

Sorry bud but I'm not going to make a parameterless constructor. There's a reason why it has a parameter - because it requires it. Removing dependencies among your objects makes your code less concise, less testable and more error prone. By having a parameterless constructor like you have done it means that you are 100% assuming that you will always have an UmbracoContext singleton, that there's a PublishedContentRequest assigned to it and that there is a PublishedContent object assigned to that... when really you can never guarantee this unless you are working within an Umbraco request.

A RenderModel may also be constructed with an arbitrary IPublishedContent in case you want to give a custom published content model to your view.

My suggestion if you want a parameterless constructor is to have your own base class like you've done and inherit from that.


Jeroen Breuer 12 Jul 2013, 04:35:32

Thanks for explaining. I'll go for the parameterless constructor base class solution :-).


Priority: Normal

Type: Feature (request)

State: Closed

Assignee: Shannon Deminick

Difficulty: Normal

Category:

Backwards Compatible: True

Fix Submitted:

Affected versions:

Due in version:

Sprint:

Story Points:

Cycle: