Copied to clipboard

Flag this post as spam?

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


  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Mar 13, 2019 @ 12:59
    David Brendel
    0

    Using AutoMapper

    Hi all,

    I'm currently trying to use custom AutoMapper profiles in v8. I'm registering the profile in a custom IUserComposer with composition.Register<Profile, CalendarToCalendarProfile>(Lifetime.Transient);.

    My profile looks currently like this:

    public class CalendarToCalendarProfile : Profile
    {
        public CalendarToCalendarProfile()
        {
            var map = CreateMap<CalendarDto, Models.Calendar>(MemberList.Destination);
            //map.ForMember()
        }
    
        public override string ProfileName => "CalendarDtoToCalendar";
    }
    

    When I now try to map my classes it can't find a registered mapper.

    I tried using Mapper.Map() and injecting IMapper to my class.

    Any suggestions?

  • Kevin Jump 2311 posts 14697 karma points MVP 7x c-trib
    Mar 13, 2019 @ 14:10
    Kevin Jump
    0

    Hi,

    I have been setting up the profiles as part of the Composer .

    so with for example in the Compose function of an IComposer class :

      composition.Register<Profile, MyCustomProfile>();
    

    with a profile class like :

    internal class MyCustomProfile : Profile
        {
            public MyCustomProfile()
            {
                // dto -> model 
                CreateMap<MyCustomModelDto, MyCustomModel>();
    
                // model -> dto
                CreateMap<MyCustomModel, MyCustomModelDto>();
            }
        }
    

    this seems to be working for me.

    the only other thing to consider is order - I set up the profile mappings first in the Composer so that they are then available in all the services/classes that might be setup later in the function

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Mar 13, 2019 @ 15:34
    David Brendel
    0

    Hi Kevin,

    how do you actually do the mapping? Do you try to get them via DI or just calling the static Mapper.Map() method?

  • Kevin Jump 2311 posts 14697 karma points MVP 7x c-trib
    Mar 13, 2019 @ 16:12
    Kevin Jump
    0

    Hi,

    calling the static Mapper.Map often in a repository.

    e.g

      return Database.Fetch<TranslationJobDto>(sql)
                    .Select(x => Mapper.Map<TranslationJob>(x));
    
  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Mar 13, 2019 @ 17:31
    David Brendel
    0

    Ok after a bit of debugging I found out that the problem is not actually caused by automapper but by IQuery that I try to use. As these are also using mappers which you can't implement yourself as they need an attribute which is internal. And tbh I'm not sure why it is needed.

  • Oleg Medianyk 1 post 71 karma points
    May 22, 2020 @ 16:38
    Oleg Medianyk
    0

    The way Automapper 9 works with Umbraco 8.6.1 for me. You need something like this in Composer for DI. However i am not sure if i have to use factory somehow.

    public class Composer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Register(GetMapper, Lifetime.Singleton);
        }
    
        public IMapper GetMapper(IFactory factory)
        {
            var config = new MapperConfiguration(cfg =>
            {
                var profiles = GetType().Assembly.GetTypes().Where(x => typeof(Profile).IsAssignableFrom(x));
                foreach (var profile in profiles)
                {
                    cfg.AddProfile(Activator.CreateInstance(profile) as Profile);
                }
            });
            config.AssertConfigurationIsValid();
            return new Mapper(config);
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft