Use AutoMapper class in Plugin

4 years ago
Is possible to use AutoMapper class in a custom plugin and how ?
















Many Thanks
4 years ago
faorV wrote:
Is possible to use AutoMapper class in a custom plugin and how ?
Many Thanks

Yes it is possible. You have to add reference of AutoMapper library in your plugin.
4 years ago
should I write my own AutoMapperStartupTask.cs and AutoMapperExtesion.cs, can you please show me the proper way to implement this, many thanks!
4 years ago
faorV wrote:
should I write my own AutoMapperStartupTask.cs and AutoMapperExtesion.cs, can you please show me the proper way to implement this, many thanks!

Yes you have to write your automapperExtention.cs.

public static IList<TDestination> MapTo<TSource, TDestination>(this IList<TSource> source)
        {
            Mapper.CreateMap<TSource, TDestination>();
            return Mapper.Map<IList<TSource>, IList<TDestination>>(source);
        }

        public static TDestination MapTo<TSource, TDestination>(this TSource source)
        {
            Mapper.CreateMap<TSource, TDestination>();
            return Mapper.Map<TSource,TDestination>(source);
        }

I have write this code to automap my objects.
4 years ago
Thanks for your answer and sorry for the negative vote (it wasn't my intention)
4 years ago
Its Ok :)
4 years ago
Many thanks, now is working perfectly!
4 years ago
It looks in 3.80 you have upgraded AutoMapper and the approach with it's initialization.
Could you demo how to add custom mappings on a plugin level?

Thank you in advance)
4 years ago
danconia wrote:
It looks in 3.80 you have upgraded AutoMapper and the approach with it's initialization.
Could you demo how to add custom mappings on a plugin level?

Thank you in advance)

Just implement IMapperConfiguration interface. You can do absolutely the same way as it's done in the  \Nop.Admin\Infrastructure\Mapper\AdminMapperConfiguration.cs file
4 years ago
a.m. wrote:
Just implement IMapperConfiguration interface. You can do absolutely the same way as it's done in the  \Nop.Admin\Infrastructure\Mapper\AdminMapperConfiguration.cs file


I'm sorry but in 3.80 there is no IMapperConfiguration interface yet, In 3.80  we have the following


    public static class AutoMapperConfiguration
    {
        private static MapperConfiguration _mapperConfiguration;
        private static IMapper _mapper;

        /// <summary>
        /// Initialize mapper
        /// </summary>
        public static void Init()
        {
            _mapperConfiguration = new MapperConfiguration(cfg =>{...});
            _mapper = _mapperConfiguration.CreateMapper();
        }
        public static IMapper Mapper
        {
            get
            {
                return _mapper;
            }
        }
        public static MapperConfiguration MapperConfiguration
        {
            get
            {
                return _mapperConfiguration;
            }
        }


I see only way to do the same AutoMapperConfiguration in each plugin and use it in plugin extensions but as i understand in 3.90 you will change the approach and introduce IMapperConfiguration.