Question about too much logic in razor code - Umbraco V7 - best practice MVC pattern
Hi,
I am building an Umbraco site.
It is V7 and am using Visual Studio.
I am implementing logic to manipulate how the content is rendered, like reordering lists of fields. When I build a conventional MVC application I would put this logic in the Model or some in the Controller.
With Umbraco V7 where is the best practice way to put this logic which could be complex?
Because here, we don't have the conventional Model and Controller directies in Visual Studio do we? Like the routing is done through Umbraco I think.
At the most basic level I used to have a "loader" class that would go off and populate a model for me and consume that on the page.
Currently I have a customc lass the inherits from UmbracoViewPage that can use AutoMapper to map an IPublishedContent to a view model. I'm manually mapping the fields in my mapper because the performance overhead was bad doing it with reflection.
public abstract class StormViewPage<T> : UmbracoViewPage where T : class
{
private T model;
public new T Model
{
get
{
return model ?? SetModel(base.Model);
}
}
private T SetModel(IPublishedContent content)
{
model = content.As<T>();
return model;
}
}
public class RichTextChildModelMapper : UmbracoPublishedContentMapper<RichTextChildModel>
{
protected override RichTextChildModel Transform(IPublishedContent source)
{
if (source.IsNullOrEmpty())
return RichTextChildModel.NewNull();
return new RichTextChildModel
{
BodyText = source.Get(Up.Common.BodyText),
Name = source.Name
};
}
}
public static class PublishedContentExtensions
{
public static T As<T>(this IPublishedContent content)
{
return Mapper.Map<IPublishedContent, T>(content);
}
}
Question about too much logic in razor code - Umbraco V7 - best practice MVC pattern
Hi,
I am building an Umbraco site.
It is V7 and am using Visual Studio.
I am implementing logic to manipulate how the content is rendered, like reordering lists of fields. When I build a conventional MVC application I would put this logic in the Model or some in the Controller.
With Umbraco V7 where is the best practice way to put this logic which could be complex?
Because here, we don't have the conventional Model and Controller directies in Visual Studio do we? Like the routing is done through Umbraco I think.
Thanks for any advice!
Damon,
Take a look at the excellent mvc hybrid starterkit, has some good practices etc https://our.umbraco.org/projects/developer-tools/hybrid-framework/
Comment author was deleted
You could hijack by template or document type and you get to use a controller like MVC.
https://github.com/kgiszewski/LearnUmbraco7/blob/master/Chapter%2006%20-%20Surface%2C%20WebAPI%20and%20RenderMVC%20Controllers/01%20-%20RenderMVC%20Controllers.md
At the most basic level I used to have a "loader" class that would go off and populate a model for me and consume that on the page.
Currently I have a customc lass the inherits from UmbracoViewPage that can use AutoMapper to map an IPublishedContent to a view model. I'm manually mapping the fields in my mapper because the performance overhead was bad doing it with reflection.
Like this.
There is also a package called Ditto that I think does the full Automapper pipeline https://our.umbraco.org/projects/developer-tools/ditto/
is working on a reply...