我想很多后端开发者,纠结于如何在Dto及表实体中做属性关系映射,因为真的太繁琐了。,
⒈如何使用?
Mapper.Initialize(cfg => cfg.CreateMap<UsersInputDto, Users>());
UsersInputDto input = new UsersInputDto()
{
id = , firstname = "fan", lastname = "qi", uname = "fanqisoft", pwd = "admin", enabled =
};
Users user = Mapper.Map<Users>(input);
⒉映射前或映射后进行操作
首先附上实体类
using System;
using System.Collections.Generic;
using System.Text; namespace AutoMapperTest.Entities
{
public class Users
{
public int id { get; set; }
public string fullname { get; set; }
public int? age { get; set; }
public string username { get; set; } public string password { get; set; }
public int? enabled { get; set; } public override string ToString()
{
return $"用户ID:{this.id} \n用户姓名:{this.fullname}\n用户名:{this.username} \n用户密码:{this.password} \n用户是否启用:{(this.enabled==1?'是':'否')}";
}
}
}
InputDto
using System;
using System.Collections.Generic;
using System.Text; namespace AutoMapperTest.Entities
{
public class UsersInputDto
{
public int id { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public int? age { get; set; }
public string uname { get; set; } public string pwd { get; set; }
public int? enabled { get; set; }
}
}
当前端InputDto传到后端时,我需要将Dto中的firstname及lastname合并转换为数据表中的fullname
Mapper.Initialize(cfg =>
{
cfg.CreateMap<UsersInputDto, Users>().BeforeMap((dto, ent) => ent.fullname = dto.firstname + "_" + dto.lastname);
});
⒊条件映射,必须必要的条件后才会映射属性。
Mapper.Initialize(cfg =>
{
cfg.CreateMap<UsersInputDto, Users>().ForMember(d => d.age, u => u.Condition(s => s.age >= && s.age <= ));
});
⒋属性对应映射,Dto中属性名 != 数据表属性名
Mapper.Initialize(cfg =>
{
cfg.CreateMap<UsersInputDto, Users>().ForMember(d => d.username, u => u.MapFrom(s => s.uname))
.ForMember(d => d.password, u => u.MapFrom(s => s.pwd));
});
⒌使用配置文件?实现Profile类并在构造器中初始化你的配置。
using AutoMapper;
using AutoMapperTest.Entities;
using System;
using System.Collections.Generic;
using System.Text; namespace AutoMapperTest.AutoMapper
{
public class AutoMapperConfig:Profile
{
public AutoMapperConfig()
{
CreateMap<UsersInputDto, Users>();
}
}
}
Mapper.Initialize(cfg =>
{
cfg.CreateMap<UsersInputDto, Users>().ForMember(d => d.username, u => u.MapFrom(s => s.uname))
.ForMember(d => d.password, u => u.MapFrom(s => s.pwd));
cfg.AddProfile<AutoMapperConfig>(); //添加一个配置文件
});
⒍Dto中数据类型和数据表不一致那就自定义转换器吧。
using AutoMapper;
using AutoMapperTest.Entities;
using System;
using System.Collections.Generic;
using System.Text; namespace AutoMapperTest.AutoMapper
{
public class UsersConverter:ITypeConverter<Users,UsersOutputDto>
{
public UsersOutputDto Convert(Users source, UsersOutputDto destination, ResolutionContext context)
{
string[] names = source.fullname.Split("_");
return new UsersOutputDto()
{
id = source.id,
firstname = names[],
lastname = names[]
};
}
}
}
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Users, UsersOutputDto>().ConvertUsing<UsersConverter>();
});
Users users = new Users()
{
id = , fullname = "fan_qi",age = ,username = "fanqisoft",password ="admin",enabled =
};
UsersOutputDto output = Mapper.Map<UsersOutputDto>(users);
⒎附一个可用的静态工具类
using AutoMapper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text; namespace AutoMapperTest.AutoMapper
{
/// <summary>
/// AutoMapper扩展帮助类
/// </summary>
public static class AutoMapperHelper
{
/// <summary>
/// 类型映射
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static T MapTo<T>(this object obj)
{
if(obj == null)
{
return default(T);
}
Mapper.Initialize(cfg =>
{
cfg.CreateMap(obj.GetType(), typeof(T));
});
return Mapper.Map<T>(obj);
} /// <summary>
/// 集合列表类型映射
/// </summary>
public static List<TDestination> MapToList<TDestination>(this IEnumerable source)
{
foreach (var first in source)
{
var type = first.GetType();
Mapper.Initialize(cfg =>
{
cfg.CreateMap(type, typeof(TDestination));
});
break;
}
return Mapper.Map<List<TDestination>>(source);
}
/// <summary>
/// 集合列表类型映射
/// </summary>
public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
{
//IEnumerable<T> 类型需要创建元素的映射
Mapper.Initialize(cfg =>
{
cfg.CreateMap<TSource, TDestination>();
});
return Mapper.Map<List<TDestination>>(source);
}
/// <summary>
/// 类型映射
/// </summary>
public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
where TSource : class
where TDestination : class
{
if (source == null) return destination;
Mapper.Initialize(cfg =>
{
cfg.CreateMap<TSource, TDestination>();
});
return Mapper.Map(source, destination);
}
/// <summary>
/// DataReader映射
/// </summary>
public static IEnumerable<T> DataReaderMapTo<T>(this IDataReader reader)
{
Mapper.Reset();
Mapper.Initialize(cfg =>
{
cfg.CreateMap<IDataReader, IEnumerable<T>>();
});
return Mapper.Map<IDataReader, IEnumerable<T>>(reader);
}
}
}