(精华)2020年7月3日 ASP.NET Core AutoMapper实现类的相互映射(工具版)

需要安装如下包

AutoMapper
public void ConfigureServices(IServiceCollection services)
{
     
    #region 自动映射拥有MapAttribute的类
    services.AddAutoMapper();
    #endregion
}
/// 
/// 使用AutoMapper自动映射拥有MapAttribute的类
/// 
/// 服务集合
/// 自定义配置
public static IServiceCollection AddAutoMapper(this IServiceCollection services, Action<IMapperConfigurationExpression> configure = null)
{
     
    List<(Type from, Type[] targets)> maps = new List<(Type from, Type[] targets)>();

    maps.AddRange(GlobalData.AllFxTypes.Where(x => x.GetCustomAttribute<MapAttribute>() != null)
        .Select(x => (x, x.GetCustomAttribute<MapAttribute>().TargetTypes)));

    var configuration = new MapperConfiguration(cfg =>
    {
     
        maps.ForEach(aMap =>
        {
     
            aMap.targets.ToList().ForEach(aTarget =>
            {
     
                cfg.CreateMap(aMap.from, aTarget).IgnoreAllNonExisting(aMap.from, aTarget).ReverseMap();
            });
        });

        cfg.AddMaps(GlobalData.AllFxAssemblies);

        //自定义映射
        configure?.Invoke(cfg);
    });

#if DEBUG
    //只在Debug时检查配置
    configuration.AssertConfigurationIsValid();
#endif
    services.AddSingleton(configuration.CreateMapper());

    return services;
}
[Map(typeof(Base_User))]
public class Base_UserDTO : Base_User
{
     
    public string RoleNames {
      get => string.Join(",", RoleNameList ?? new List<string>()); }
    public List<string> RoleIdList {
      get; set; }
    public List<string> RoleNameList {
      get; set; }
    public RoleTypes RoleType
    {
     
        get
        {
     
            int type = 0;

            var values = typeof(RoleTypes).GetEnumValues();
            foreach (var aValue in values)
            {
     
                if (RoleNames.Contains(aValue.ToString()))
                    type += (int)aValue;
            }

            return (RoleTypes)type;
        }
    }
    public string DepartmentName {
      get; set; }
    public string SexText {
      get => Sex.GetDescription(); }
    public string BirthdayText {
      get => Birthday?.ToString("yyyy-MM-dd"); }
}
/// 
/// 系统用户表
/// 
[Table("Base_User")]
public class Base_User
{
     

    /// 
    /// 主键
    /// 
    [Key, Column(Order = 1)]
    public String Id {
      get; set; }

    /// 
    /// 创建时间
    /// 
    public DateTime CreateTime {
      get; set; }

    /// 
    /// 创建人Id
    /// 
    public String CreatorId {
      get; set; }

    /// 
    /// 否已删除
    /// 
    public Boolean Deleted {
      get; set; }

    /// 
    /// 用户名
    /// 
    public String UserName {
      get; set; }

    /// 
    /// 密码
    /// 
    public String Password {
      get; set; }

    /// 
    /// 姓名
    /// 
    public String RealName {
      get; set; }

    /// 
    /// 性别
    /// 
    public Sex Sex {
      get; set; }

    /// 
    /// 出生日期
    /// 
    public DateTime? Birthday {
      get; set; }

    /// 
    /// 所属部门Id
    /// 
    public String DepartmentId {
      get; set; }
}

public enum Sex
{
     
    [Description("男人")]
    Man = 1,

    [Description("女人")]
    Woman = 0
}
public class MapAttribute : Attribute
{
     
    public MapAttribute(params Type[] targetTypes)
    {
     
        TargetTypes = targetTypes;
    }
    public Type[] TargetTypes {
      get; }
}

具体的其他相关类请移到类库中去查找本人博客都有

你可能感兴趣的:(#,ASP.NET,Core,c#,asp.net,后端)