首页 技术 正文
技术 2022年11月20日
0 收藏 886 点赞 4,026 浏览 4340 个字

我想很多后端开发者,纠结于如何在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);
}
}
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295