Copied to clipboard

Flag this post as spam?

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


  • Peter Laurie 42 posts 116 karma points
    Mar 14, 2019 @ 15:18
    Peter Laurie
    0

    Umbraco 8 Custom core Mappers

    Hi, I have been looking for any documentation on add my own custom mappers (using AutoMapper into Umbraco 8 on start up.

    I tried the following code, but the error I got back was that AutoMapper can only be initialised once.

    public class AutoMapperComponent : IComponent
      {
        // initialize: runs once when Umbraco starts
        public void Initialize()
        {
          InitAutoMapper();
        }
    
        public void InitAutoMapper()
        {
          Mapper.Initialize(cfg => cfg.CreateMap<CmtRestOptions, CmtRestConfig>());
        }
    
        // terminate: runs once when Umbraco stops
        public void Terminate()
        {
        }
      }
    

    I have then come across the following code by looking at what is exposed by composition in Visual Studio, and came across AddCoreMappers

     [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
      public class ApplicationStartUp : IUserComposer
      {
        public void Compose(Composition composition)
        {
          // Append our component to the collection of Components
          // It will be the last one to be run
          composition.Mappers().AddCoreMappers<AutoMapperComponent>(); // look into add core mappers
        }
      }
    

    So far I have found no documantation on implementing my own Maps using this function.

    If anyone has any information concerning this, or pointers in the right direction, that would be most welcome.

    Kind regards, Peter

  • Kevin Jump 2311 posts 14697 karma points MVP 7x c-trib
    Mar 14, 2019 @ 15:23
    Kevin Jump
    5

    [Update: 9th July 2019: Since Umbraco 8.1 Automapper has been removed as a dependency https://umbraco.com/blog/umbraco-81-release/?_ga=2.125281161.2080844306.1562584414-163467214.1557423111#changes]

    Hi Peter,

    Along with the Umbraco Updated AutoMapper has been upgraded to the latest v8 version and that has changed things a little.

    the prescribed way to add mappers appears to be to use a profile class

    eg :

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

    these profiles define the mappings for you and then you register the profile in your composer.

    composition.Register<Profile, MyCustomProfile>();
    

    this will then let you use Mapper.Map across your code. as before.

  • MaartenVissers 32 posts 150 karma points
    Apr 03, 2020 @ 06:49
    MaartenVissers
    0

    He, thanks this helped me! But I can't seem to do Mapper.Map?

    enter image description here

    Problem solved: I had to inject Mapper, since it's not static anymore. Configured like this:

     var config = new AutoMapper.MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyCustomProfile());
            });
      var mapper = config.CreateMapper();
      composition.Register(mapper);
    
  • Kamal Hamzat 17 posts 137 karma points
    Oct 18, 2021 @ 13:25
    Kamal Hamzat
    0

    Hi Maarten,

    After registering the Auto Mapper in the composer as follows, I am still unable to use Mapper.Map in my code.

    public void Compose(Composition composition)
            {
                var config = new AutoMapper.MapperConfiguration(cfg =>
                {
                    cfg.AddProfile(new MapperProfile());
                });
    
                var mapper = config.CreateMapper();
    
                composition.Register(mapper);
            }
    

    When I do this :

    var viewModel = Mapper.Map<HomePageViewModel>(Model);
    

    Do you have to do anything on your controller page to use it?

    Thanks

  • Emma Garland 41 posts 123 karma points MVP 6x c-trib
    Apr 05, 2019 @ 09:36
    Emma Garland
    0

    Thanks both - can confirm, this works to enable me to add extra AutoMapper maps in Umbraco v8.0.1

Please Sign in or register to post replies

Write your reply to:

Draft