Copied to clipboard

Flag this post as spam?

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


  • Jesper Ordrup 1000 posts 1478 karma points MVP
    Nov 05, 2014 @ 22:37
    Jesper Ordrup
    0

    TypedMedia issues?

    Hi all,

    The first works while the second doesnt:

    var media = Umbraco.TypedMedia((int)Convert.ToInt32(mediaItem));
    var media2 = Umbraco.TypedMedia(Convert.ToInt32(mediaItem));

    Why is it different? Both returns something that looks the same but the latter looks like a dynamic?

    best

    Jesper

     

  • Stephen 767 posts 2268 karma points c-trib
    Nov 05, 2014 @ 22:54
    Stephen
    1

    What's the type of mediaItem? Is it a dynamic?

    Once something is dynamic, everything is dynamic, so Convert.ToInt32(mediaItem) would be a dynamic (OK, an int really, but a dynamic as far as the compiler is concerned). So Umbraco.TypedMedia(Convert.ToInt32(mediaItem)) is also a dynamic (OK, an IPublishedContent really, but a dynamic as far as the compiler is concerned). Using the (int) cast breaks the dynamic propagation, so Umbraco.TypedMedia((int)Convert.ToInt32(mediaItem)) is considered an IPublishedContent by the compiler.

    Making sense?

  • Jesper Ordrup 1000 posts 1478 karma points MVP
    Nov 05, 2014 @ 23:12
    Jesper Ordrup
    0

    Thanks Stephen. That makes sense. :)

    Compilerwise sense. But things like this creates a lot of trouble for the less developerminded "implementers".

    I'm thinking that Umbraco might need a high level api that can be used in razor for the trivial stuff that only takes primitive data types as parameter and return values (and arrays) and fails silently. 

    Edit: I actually cant believe I'm suggesting this as I think theres already too many ways of doing the same thing but nevertheless ...

  • Stephen 767 posts 2268 karma points c-trib
    Nov 05, 2014 @ 23:16
    Stephen
    2

    Just do not, ever, use dynamics.

  • Jesper Ordrup 1000 posts 1478 karma points MVP
    Nov 05, 2014 @ 23:28
    Jesper Ordrup
    0

    Well it's there. And it requires dev skills to avoid it  :)

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Nov 05, 2014 @ 23:29
    Chriztian Steinmeier
    0

    I totally follow you, Jesper.

    Whenever you (the royal you - anybody) have to use .ToString() or need to wrap everything in .IsNullOrEmpty() or that hideous .GetProperty<T>("propertyName") etc. in a View, the tools should just throw a #fail hashtag :-)

    To me, they almost always signal that the controller could do a better job, preparing the actual data for the view... (I'm using those terms loosely, but that's the gist of it).

    /Chriztian

  • Casey Neehouse 1339 posts 483 karma points MVP 2x admin
    Nov 06, 2014 @ 03:06
    Casey Neehouse
    1

    @Jesper - Sounds like it's time for an extension library to provide a proof of concept.  :)  Reminds me of the days of those xslt extensions.  :P

  • Jesper Ordrup 1000 posts 1478 karma points MVP
    Nov 06, 2014 @ 03:58
    Jesper Ordrup
    1

    And it's exactly the same (low) sofistication level needed! And just a App_Code thing. Ground rules might be:

    - no objects, no exceptions
    - simple parameters and return (string, int)
    - failover to default values ... no 

     

    Simple.Media

    string value = Media.GetFileUrl(id [, defaultvalue])
    string value = Media.GetFileName(id [, defaultvalue])
    string value = Media.GetName(id [, defaultvalue]) 
    string value = Media.GetFileExtension(id [, defaultvalue])
    string value = Media.GetCropped(id, cropAlias [, defaultvalue])
    string value = Media.GetString(id, propertyAlias [, defaultvalue])
    int value = Media.GetInt(id, propertyAlias [, defaultvalue])
    int[] value = Media.GetChildrenIds(id) 

     

    Simple.Content

    string value = Content.GetString(id, propertyAlias [, defaultvalue])
    int value = Content.GetInt(id, propertyAlias [, defaultvalue])
    string value = Content.GetUrl(id [, defaultvalue])
    string value = Content.GetName(id, [, defaultvalue])
    int[] value = Content.GetChildrenIds(id)
    bool value = Content.HasAccess(id)
    etc ...

     

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies