Copied to clipboard

Flag this post as spam?

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


  • Jason Espin 368 posts 1335 karma points
    Mar 26, 2017 @ 13:13
    Jason Espin
    0

    Mapping Umbraco properties to custom models and mocking the Umbraco context

    Hi all,

    I've been away a bit from Umbraco and have been working on the dark side in Sitecore :-(

    As part of some of the things I have learned with Sitecore I am trying to setup IOC with Umbraco and automatic model mapping (where my Umbraco templates and properties are mapped to interfaces which in turn map to concrete model classes I can use in my views).

    In Sitecore I have used CastleWindsor for IOC and GlassMapper for model mapping but it would seem GlassMapper is no longer available for Umbraco.

    Instead I have setup StructureMap for my controllers using the following excellent tutorial:

    https://blog.polarbits.co/2015/02/18/getting-started-with-umbraco-7-and-structuremap-v3/

    This should allow me to create a wrapper around the Umbraco context so that I can unit test easier.

    The main problem I have now is the model resolving. Does anyone know of a good solution for this in Umbraco?

    Cheers,

    Jason

  • Jamie Pollock 174 posts 853 karma points c-trib
    Mar 27, 2017 @ 02:31
    Jamie Pollock
    0

    Hey Jason,
    Not sure this entirely solves your problem. You should definitely have a look into Umbraco Ditto.

    It works on the principle of decorating view models (or domain models if you want) with attributes. A lot of common cases are provided for you with the package but you can always roll your own.

    Also you should consider that Umbraco since v7.4(ish?) has Umbraco ModelsBuilder which creates strongly typed Umbraco Models out of the box based on your document types. I'd consider these more domain model rather than view model as they might need a bit of tinkering before they're ready to be sent out to your views but it's really clever stuff and a lot better than writing GetPropertyValue<string>("myProperty") all the time. :)

    If you'd like a comparison of the two, check out Lars-Erick's blog post on the matter. I'd recommend giving it a read anyway as it's a nice primer to both.

    I hope this helps.

    Thanks,
    Jamie

Please Sign in or register to post replies

Write your reply to:

Draft