automapper true false to Y N and reverse

核能气质少年 提交于 2020-01-25 10:00:27

问题


We currently have a viewmodel (custom c# class) which has two properties is_active and is_deleted which are bool. the entity class which is saved/retrieved by the db calls has those fields as string, as in the DB they are stored to 'Y' and 'N'.

Currently we have this for the AutoMapper mapping:

            CreateMap<classOne, classTwo>()
            .ForPath(d => d.IS_ACTIVE, opt => opt.MapFrom(s => s.IS_ACTIVE == "Y" ? true : false))
            .ForPath(d => d.IS_DELETED, opt => opt.MapFrom(s => s.IS_DELETED == "Y" ? true : false));

        CreateMap<classTwo, classOne>()
            .ForPath(d => d.IS_ACTIVE, opt => opt.MapFrom(s => s.IS_ACTIVE ? "Y" : "N"))
            .ForPath(d => d.IS_DELETED, opt => opt.MapFrom(s => s.IS_DELETED ? "Y" : "N"));

This works, but is there a cleaner way to do this? so going from classOne to classTwo and reverse work? We will have many of these so trying to find a cleaner approach.

来源:https://stackoverflow.com/questions/55616296/automapper-true-false-to-y-n-and-reverse

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!