Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重构AutoMapper模块,解决复杂映射配置优先级低的问题 #230

Closed
gmf520 opened this issue Apr 7, 2021 · 0 comments
Closed
Labels
Breaked Changes ⚡ 更新有破坏性,对现有业务实现有较大影响 Feature 🔨 新功能,新特性 Finished ✔️ 实现并完工
Milestone

Comments

@gmf520
Copy link
Member

gmf520 commented Apr 7, 2021

您的功能请求与现有问题有关吗?请描述

automapper的如果有MapFromAttribute和实现IAutoMapperConfiguration,后者会配置会失效 #109

描述您想要的需求方案

由于IAutoMapperConfiguration的优先级比Profile的方式低,无法提升IAutoMapperConfiguration的优先级
统一改为使用Profile的方式来管理对象映射配置
更改如下:

  • 移除接口IAutoMapperConfiguration
  • 在接口IMapTuple增加Order属性,用于配置多个Profile的执行顺序
  • 添加IMapTuple的实现基类AutoMapperTupleBase,其他模块的对象映射均以实现此基类为准
  • MapTupleProfile更名为MapFromAndMapToProfile,Order默认为-9999,优先执行[MapFrom], [MapTo]的映射
  • 原先IAutoMapperConfiguration的实现类改为实现基类AutoMapperTupleBase,Order默认为0,其配置可以覆盖[MapFrom], [MapTo]中的配置
    一个AutoMapperTupleBase实现类的示例
old:
    public class AutoMapperConfiguration : IAutoMapperConfiguration
    {
        /// <summary>
        /// 创建对象映射
        /// </summary>
        /// <param name="mapper">映射配置表达</param>
        public void CreateMaps(MapperConfigurationExpression mapper)
        {
            mapper.CreateMap<Role, RoleNode>().ForMember(rn => rn.RoleId, opt => opt.MapFrom(r => r.Id))
                .ForMember(rn => rn.RoleName, opt => opt.MapFrom(r => r.Name));
        }
    }
new:
    public class AutoMapperConfiguration : AutoMapperTupleBase
    {
        /// <summary>
        /// 创建对象映射
        /// </summary>
        public override void CreateMap()
        {
            CreateMap<Role, RoleNode>().ForMember(rn => rn.RoleId, opt => opt.MapFrom(r => r.Id))
                .ForMember(rn => rn.RoleName, opt => opt.MapFrom(r => r.Name));
        }
    }
@gmf520 gmf520 added Breaked Changes ⚡ 更新有破坏性,对现有业务实现有较大影响 Feature 🔨 新功能,新特性 labels Apr 7, 2021
@gmf520 gmf520 added this to the v5.0.5 milestone Apr 7, 2021
gmf520 added a commit that referenced this issue Apr 7, 2021
@gmf520 gmf520 added the Finished ✔️ 实现并完工 label Apr 7, 2021
@gmf520 gmf520 closed this as completed Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaked Changes ⚡ 更新有破坏性,对现有业务实现有较大影响 Feature 🔨 新功能,新特性 Finished ✔️ 实现并完工
Projects
None yet
Development

No branches or pull requests

1 participant