AutoMapper 9.0的使用

AutoMapper于2019.8.12升级9.0
从此不再支持静态的对象转换
自己琢磨了一下,写了一套扩展
官方链接如下
https://github.com/AutoMapper/AutoMapper/tree/v9.0.0
我做了一下方便使用的简单封装
public static class AutoMapperExtension
    {
        public static IServiceCollection AddAutoMapper(this IServiceCollection service)
        {
            service.TryAddSingleton();
            service.TryAddSingleton(serviceProvider =>
            {
                var mapperConfigurationExpression = serviceProvider.GetRequiredService();
                var instance = new MapperConfiguration(mapperConfigurationExpression);

                instance.AssertConfigurationIsValid();

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

                return mapperConfiguration.CreateMapper();
            });

            return service;
        }

        public static IMapperConfigurationExpression UseAutoMapper(this IApplicationBuilder applicationBuilder)
        {
            return applicationBuilder.ApplicationServices.GetRequiredService();
        }
    }
调用则是在ConfigureServices方法内增加一行
services.AddAutoMapper();
在Configure方法内增加如下代码
            var expression = app.UseAutoMapper();
            expression.CreateMap();
这样就完成了DI版本的AutoMapper
调用例子代码如下
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(model);
        }
    }
public static class AutoMapperHelper
    {
        private static IServiceProvider ServiceProvider;

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

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

            return mapper.Map(source);
        }

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

            return mapper.Map(source);
        }
    }

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

            return AutoMapperHelper.Map(model);
        }

public static class AutoMapperHelper
    {
        ...
        public static TDestination MapTo(this TSource source)
        {
            var mapper = ServiceProvider.GetRequiredService();

            return mapper.Map(source);
        }

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

            return mapper.Map(source);
        }
    }


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

            return model.MapTo();
        }

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

        //    return model.MapTo();
        //}

你可能感兴趣的:(C#基础)