[转载]AutoMapper 9.0的改造

转载自

https://www.cnblogs.com/NCoreCoder/p/11359294.html

 

 

AutoMapper于2019.8.12升级9.0

从此不再支持静态的对象转换

自己琢磨了一下,写了一套扩展

官方链接如下

https://github.com/AutoMapper/AutoMapper/tree/v9.0.0

我做了一下方便使用的简单封装

[转载]AutoMapper 9.0的改造
    public static class AutoMapperExtension
    {
        public static IServiceCollection AddAutoMapper(this IServiceCollection service)
        {
            service.TryAddSingleton<MapperConfigurationExpression>();
            service.TryAddSingleton(serviceProvider =>
            {
                var mapperConfigurationExpression = serviceProvider.GetRequiredService<MapperConfigurationExpression>();
                var instance = new MapperConfiguration(mapperConfigurationExpression);

                instance.AssertConfigurationIsValid();

                return instance;
            });
            service.TryAddSingleton(serviceProvider =>
            {
                var mapperConfiguration = serviceProvider.GetRequiredService<MapperConfiguration>();

                return mapperConfiguration.CreateMapper();
            });

            return service;
        }

        public static IMapperConfigurationExpression UseAutoMapper(this IApplicationBuilder applicationBuilder)
        {
            return applicationBuilder.ApplicationServices.GetRequiredService<MapperConfigurationExpression>();
        }
    }
[转载]AutoMapper 9.0的改造

调用则是在ConfigureServices方法内增加一行

services.AddAutoMapper();

在Configure方法内增加如下代码

            var expression = app.UseAutoMapper();
            expression.CreateMap<Foo, FoodDto>();

这样就完成了DI版本的AutoMapper

调用例子代码如下

[转载]AutoMapper 9.0的改造
    public class ValuesController : ControllerBase
    {
        private IMapper Mapper { get; }

        public ValuesController(IMapper mapper)
        {
            Mapper = mapper;
        }

        // GET api/values
        [HttpGet]
        public FoodDto Get()
        {
            var model = new Foo()
            {
                Id = Guid.NewGuid(),
                Name = "test",
                Money = 15.0m
            };

            return Mapper.Map<FoodDto>(model);
        }
    }
[转载]AutoMapper 9.0的改造

测试结果如下

[转载]AutoMapper 9.0的改造

 

但是仅仅是这样不够的,方便使用的话,还需要一个静态Helper

代码如下

[转载]AutoMapper 9.0的改造
    public static class AutoMapperHelper
    {
        private static IServiceProvider ServiceProvider;

        public static void UseStateAutoMapper(this IApplicationBuilder applicationBuilder)
        {
            ServiceProvider = applicationBuilder.ApplicationServices;
        }

        public static TDestination Map<TDestination>(object source)
        {
            var mapper = ServiceProvider.GetRequiredService<IMapper>();

            return mapper.Map<TDestination>(source);
        }

        public static TDestination Map<TSource, TDestination>(TSource source)
        {
            var mapper = ServiceProvider.GetRequiredService<IMapper>();

            return mapper.Map<TSource, TDestination>(source);
        }
    }
[转载]AutoMapper 9.0的改造

同理在Configure方法内增加一行代码

app.UseStateAutoMapper();

增加一个Action调用一下

[转载]AutoMapper 9.0的改造
        [HttpGet("Static")]
        public FoodDto Static()
        {
            var model = new Foo()
            {
                Id = Guid.NewGuid(),
                Name = "test",
                Money = 15.0m
            };

            return AutoMapperHelper.Map<FoodDto>(model);
        }
[转载]AutoMapper 9.0的改造

调用结果如下

[转载]AutoMapper 9.0的改造

 

大功告成,其实这次更新只是一个思路上的转变,便捷性,都只是小事儿而已

 


应邀在某个汪姓大佬的要求下,增加两个扩展方法

[转载]AutoMapper 9.0的改造
    public static class AutoMapperHelper
    {
        ...
        public static TDestination MapTo<TSource, TDestination>(this TSource source)
        {
            var mapper = ServiceProvider.GetRequiredService<IMapper>();

            return mapper.Map<TSource, TDestination>(source);
        }

        public static TDestination MapTo<TDestination>(this object source)
        {
            var mapper = ServiceProvider.GetRequiredService<IMapper>();

            return mapper.Map<TDestination>(source);
        }
    }
[转载]AutoMapper 9.0的改造

测试例子改为

[转载]AutoMapper 9.0的改造
        [HttpGet("Static")]
        public FoodDto Static()
        {
            var model = new Foo()
            {
                Id = Guid.NewGuid(),
                Name = "test",
                Money = 15.0m
            };

            return model.MapTo<FoodDto>();
        }

        //[HttpGet("Static")]
        //public FoodDto Static()
        //{
        //    var model = new Foo()
        //    {
        //        Id = Guid.NewGuid(),
        //        Name = "test",
        //        Money = 15.0m
        //    };

        //    return model.MapTo<Foo, FoodDto>();
        //}
上一篇:映射2枚举值到1值


下一篇:CodeGo.net>用AutoMapper的空字符串替换为空