Copied to clipboard

Flag this post as spam?

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


  • LeszekP 27 posts 78 karma points
    Jun 15, 2015 @ 22:55
    LeszekP
    0

    Custom mapping, propertyName first letter small

    Hi,

    How to stop Umbraco mapper from making propertyAlias first letter small? If I define propertyAlias with capital letter it is ignored.

    _mapper.AddCustomMapping(typeof(ServicesViewModel).FullName, CustomMaps.MapDescedant<ServicesViewModel>, "Services");
    
    public static object MapDescedant<T>(IUmbracoMapper mapper, IPublishedContent contentToMapFrom, string propertyName,
    bool isRecursive) where T : class, new()
    {
        propertyName = camelCase 
    }.
    
  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jun 16, 2015 @ 06:30
    Andy Butland
    0

    Hi - camel cased aliases are the Umbraco default (what gets generated for you if you don't change it), so the convention is that Umbraco Mapper will try to match on camel case.

    You can override it using an attribute on a given field like this:

    [PropertyMapping(SourceProperty = "MyAliasName")]
    
  • LeszekP 27 posts 78 karma points
    Jun 16, 2015 @ 08:18
    LeszekP
    0

    Hi Andy,

    Thank you for quick response.

    I forgot to mention, that I map descendant (DocTypeAlias), not a property.

        public class HomePageViewModel : MasterViewModel
    {
        public HomePageViewModel()
        {
            NewsItem = new List<NewsItemViewModel>();
        }
    
        public IList<NewsItemViewModel> NewsItem { get; set; }
    }
    

    Umbraco names propertyName with small letter first, but DocTypeAlias with Capital letter. And the second problem is that I have to use name NewsItem or UM wont map.

    Adding:

            [PropertyMapping(SourceProperty = "Article")]
        public IList<ArticleViewModel> Articles { get; set; }
    

    does not help. Still propertyName is with small letter.

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jun 16, 2015 @ 10:19
    Andy Butland
    0

    If I'm following correctly then you are looking to map the children of your home page to your collection of NewsItems.

    In that case you will need a second mapping call - there's nothing (at least at the moment) for "auto-mapping" children of a node (main reason being would be concerned about performance if there were a lot of children/descendants, and so think it's probably better that the developer makes an explicit call to map these).

    So think you want something like:

    var vm = new HomePageViewModel();
    mapper.Map(CurrentPage, vm)
        .MapCollection(CurrentPage.Children, vm.NewsItems);
    
  • LeszekP 27 posts 78 karma points
    Jun 16, 2015 @ 11:15
    LeszekP
    0

    That is not mapping Children, but Descendants.

            public static object MapDescedants<T>(IUmbracoMapper mapper, IPublishedContent contentToMapFrom, string propertyName,
            bool isRecursive) where T : class, new()
        {
            var entities = new List<T>();
    
            var descedantCollection = contentToMapFrom.Descendants(propertyName);
            mapper.MapCollection(descedantCollection, entities);
    
            return entities;
        }
    

    In some magic way UM knows that I want to map Article (docTypeAlias)

    _mapper.AddCustomMapping(typeof(IList<ArticleViewModel>).FullName, CustomMaps.MapDescedants<ArticleViewModel>);
    

    but... makes first letter small. Umbraco uses capital letter for docTypeAlias.

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Jun 16, 2015 @ 12:03
    Andy Butland
    0

    OK - I wonder if you are trying to do something too generic here. It might work but can't say it's a little off the beaten track of what I have tried.

    The signature of the custom mapper requires the propertyName (the alias of the content field, which isn't the same thing as the document type alias of the descendent items you are looking for).

    If in your application though you have the convention that these will match, you could probably just have some code in your custom mapping function to convert the propertyName into Pascal case (i.e. upper case the first letter).

Please Sign in or register to post replies

Write your reply to:

Draft