1. 程式人生 > 其它 >.Net Core 下AutoMapper 10.0 在的使用

.Net Core 下AutoMapper 10.0 在的使用

1:首先還是需要直接Nutget兩個包:

準備兩個資料類

public class Person
    { 
        public string Name { get; set; } 
    }
    public class PersonDto
    {
        public string Name { get; set; } 
    }

2、通用註冊:

Register為了方便擴充套件,不需要再去service裡新增每一個Type

1.先建立一個Iprofile 介面

 public Interface Iprofile
    {  
    }

2.profile配置類繼承這個介面

 public class PersonProfile:Profile,Iprofile
    { 
        public PersonProfile {
            CreateMap<Person,PersonDto>();
            CreateMap<PersonDto,Person>() ;
         } 
    }    

3.建立一個MapperRegister類

 public static class MapperRegister
    {
 
        public static Type[] MapType()
        {
 
            
var allIem = Assembly .GetEntryAssembly() .GetReferencedAssemblies() .Select(Assembly.Load) .SelectMany(y => y.DefinedTypes) .Where(type => typeof(IProfile).GetTypeInfo().IsAssignableFrom(type.AsType())); List
<Type> allList = new List<Type>(); foreach (var typeinfo in allIem) { var type = typeinfo.AsType(); allList.Add(type); } Type[] alltypes = allList.ToArray(); return alltypes; } }

4.更改 StartUp.cs->AddAutoMapper

public void ConfigureServices(IServiceCollection services)
        { 
          services.AddAutoMapper(MapperRegister.MapType);
        }

所以繼承IProfile的配置類都會被自動註冊到AutoMapper裡

2、通用的方法:AutoMapperHelper

此處AutoMapperHelper有兩個意義,一,不需要在每個呼叫mapper方法的類裡再去呼叫IMapper,由Helper 統一呼叫一次。二,對映實現的寫法更易讀(二處的意義現在不太大,9.0之前的Helper方法可以節約三行程式碼,但9.0更新之後,現在原生的mapper也只需要一行程式碼)

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);
        }
 
        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);
        }
 
        public static List<TDestination> MapToList<TDestination>(this IEnumerable source)
        {
            var mapper = ServiceProvider.GetRequiredService<IMapper>();
            return mapper.Map<List<TDestination>>(source);
        }
 
        
        public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
        {
            var mapper = ServiceProvider.GetRequiredService<IMapper>();
            return mapper.Map<List<TDestination>>(source);
       }
}

在 Startup.cs 配置一下

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
           ....
            app.UseStateAutoMapper();
       }

使用

public PersonDto Get()
{
   ...
    PersonDto dto=Person.MapTo<PersonDto>();
    return dto;
}
 
//集合
public List<PersonDto> GetList()
{
 ...
    List<PersonDto> dtoList=Personlist.MapToList<PersonDto>();
     return dtoList;
}