1.普通的映射。
public class UserInfo { public int id { get; set; } public string name { get; set; } public string address { get; set; } } public class UserInfoDTO { public string name { get; set; } public string address { get; set; } } var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>();
UserInfoDTO userdto = mapper.Map(user);
2.有外键关联,需要映射出外键所带名字
public class UserInfo { public int id { get; set; } public string name { get; set; } public string address { get; set; } public Teacher teacher { get; set; } } public class Teacher { public int id { get; set; } public string name { get; set; } } public class UserInfoDTO { public int id { get; set; } public string name { get; set; } public string teacher { get; set; } } var user = new UserInfo { id = 12, name = "张三", address = "北京", teacher = new Teacher { id = 11, name = "王五" } }; var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>( new DefaultMapConfig() .ConvertUsing<Teacher, string>(t => t.name) ); UserInfoDTO userdto = mapper.Map(user);
3.两个实体之间名字不一致,需要映射。
public class UserInfo { public int id { get; set; } public string name { get; set; } public string address { get; set; } } public class UserInfoDTO { public int id { get; set; } public string name { get; set; } public string userAddress { get; set; } } var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>( new DefaultMapConfig() .MatchMembers((x, y) => { if (x == "address" && y == "userAddress") { return true; } return x == y; }) ); UserInfoDTO userdto = mapper.Map(user);
4.需要对某一个字段进行特殊处理
public class UserInfo { public int id { get; set; } public string name { get; set; } public string address { get; set; } } public class UserInfoDTO { public string id { get; set; } public string name { get; set; } public string userAddress { get; set; } public string userJson { get; set; } } var user = new UserInfo { id = 12, name = "张三", address = "北京" }; var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>( new DefaultMapConfig() .PostProcess<UserInfoDTO>((value, state) => { //在id编号前加上今年的年份 value.id = DateTime.Now.ToString("yyyy") + value.id; //实体的json格式 value.userJson = "{"id":"" + value.id + "","name":"" + value.name + ""}"; return value; }) ); UserInfoDTO userdto = mapper.Map(user);
5.忽略掉某个字段的映射
public class UserInfo { public int id { get; set; } public string name { get; set; } public string address { get; set; } } public class UserInfoDTO { public string id { get; set; } public string name { get; set; } public string address { get; set; } } var user = new UserInfo { id = 12, name = "张三", address = "北京" }; var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>( new DefaultMapConfig() .IgnoreMembers<UserInfo, UserInfoDTO>(new string[] { "name" }) ); UserInfoDTO userdto = mapper.Map(user);
6.给空元素赋默认值
public class UserInfo { public int id { get; set; } public string name { get; set; } public string address { get; set; } public DateTime? godate { get; set; } } public class UserInfoDTO { public string id { get; set; } public string name { get; set; } public string address { get; set; } public DateTime godate { get; set; } } var user = new UserInfo { id = 12, name = "张三", address = null, godate = null }; var mapper = ObjectMapperManager.DefaultInstance.GetMapper<UserInfo, UserInfoDTO>( new DefaultMapConfig() //如果日期为空设置为当前时间 .NullSubstitution<DateTime?, DateTime>((value) => DateTime.Now) //如果string类型为null赋值为“” .NullSubstitution<string, string>((value) => "") ); UserInfoDTO userdto = mapper.Map(user);
常用的就上面几点
private IMapper _mapper; public IMapper Mapper { get { if (null == _mapper) { _mapper = AssemblerIoc.GetMapper(); } return _mapper; } }
public class Mapper { public static IMapper _mapper; public static IMapper GetMapper { get { if (_mapper == null) { _mapper = new MapperImpl(); } return _mapper; } } }
public interface IMapper { TDestination Map<TSource, TDestination>(TSource tSource); IEnumerable<TDestination> MapperGeneric<TSource, TDestination>(IEnumerable<TSource> tSources); IList<TDestination> MapperGeneric<TSource, TDestination>(IList<TSource> tSources); }
public class MapperImpl : IMapper { public TDestination Map<TSource, TDestination>(TSource tSource) { if (tSource == null) return default(TDestination); var mapper = ObjectMapperManager.DefaultInstance.GetMapper<TSource, TDestination>(); return mapper.Map(tSource); } public IEnumerable<TDestination> MapperGeneric<TSource, TDestination>(IEnumerable<TSource> tSources) { if (tSources == null) return null; IList<TDestination> tDestinations = new List<TDestination>(); foreach (var tSource in tSources) { tDestinations.Add(Map<TSource, TDestination>(tSource)); } return tDestinations; } public IList<TDestination> MapperGeneric<TSource, TDestination>(IList<TSource> tSources) { if (tSources == null) return null; IList<TDestination> tDestinations = new List<TDestination>(); foreach (var tSource in tSources) { tDestinations.Add(Map<TSource, TDestination>(tSource)); } return tDestinations; } }