Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Damon 217 posts 288 karma points
    Jul 16, 2015 @ 15:57
    Damon
    0

    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!

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jul 16, 2015 @ 16:10
    Ismail Mayat
    0

    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

    Jul 16, 2015 @ 16:17
  • Valerie 67 posts 163 karma points
    Jul 17, 2015 @ 10:07
    Valerie
    0

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

    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/

Please Sign in or register to post replies

Write your reply to:

Draft