forward from
https://www.cnblogs.com/NCoreCoder/p/11359294.html
AutoMapper 2019.8.12 upgrade to 9.0
No longer supports static objects conversion
On her own for a moment, to write a set of extensions
Official link below
https://github.com/AutoMapper/AutoMapper/tree/v9.0.0
I do a bit of simple and easy to use package
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>(); } }
Calling is to add a line in the ConfigureServices method
services.AddAutoMapper();
Add the following code in the method Configure
var expression = app.UseAutoMapper(); expression.CreateMap<Foo, FoodDto>();
This completes the DI version of AutoMapper
Call the following example code
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); } }
Test results are as follows
But only this is not enough, easy to use, it also requires a static Helper
code show as below
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); } }
Similarly the increase in the line of code method Configure
app.UseStateAutoMapper();
Add a call about Action
[HttpGet("Static")] public FoodDto Static() { var model = new Foo() { Id = Guid.NewGuid(), Name = "test", Money = 15.0m }; return AutoMapperHelper.Map<FoodDto>(model); }
Call the following results
Done, in fact, this update is only a shift in thinking, convenience, it is just a small thing
He was invited at the request of a big brother surnamed Wang, adding two extension methods
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); } }
Test case changed
[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>(); //}