Use AutoMapper class in Plugin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Il y a 8 ans
Is possible to use AutoMapper class in a custom plugin and how ?
















Many Thanks
Il y a 8 ans
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.
Il y a 8 ans
should I write my own AutoMapperStartupTask.cs and AutoMapperExtesion.cs, can you please show me the proper way to implement this, many thanks!
Il y a 8 ans
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.
Il y a 8 ans
Thanks for your answer and sorry for the negative vote (it wasn't my intention)
Il y a 8 ans
Its Ok :)
Il y a 8 ans
Many thanks, now is working perfectly!
Il y a 7 ans
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)
Il y a 7 ans
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
Il y a 7 ans
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.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.