[Practice Note] 3.Abp入门:Dto与实体模型的映射-AutoMapper

Demo项目地址:https://download.csdn.net/download

新建实体模型与Dto对象传输模型

在这里插入图片描述

namespace ABPTest.EntityModel
{
    [Table("Common_Participant")] //数据库表名,会议参与者表
    public class Participant:FullAuditedEntity<int>
    {
    	//姓名
        public string Name { get; set; }
        //年龄
        public int Age { get; set; }
        //性别 1-Male 2-FeMale
        public int Sex { get; set; }
        //忽略字段,后面用到
        public string Ignore { get; set; }
        //备注
        public string Remark { get; set; }
    }
}

添加完实体在EntityFramwork中的Context上下文类中加入Participant实体,然后数据迁移!不清楚操作,可以参考之前的篇章 数据迁移

接下来我们添加Dto传输对象

  1. AddParticipantDto.cs
  2. GetParticipantDto.cs

在这里插入图片描述

namespace ABPTest.Participant.Dto
{
	//[AutoMapFrom(typeof(EntityModel.Participant))]  这段意味着实体自动映射到Dto模型,下面的自定义映射代码可以删除
    public class AddParticipantDto
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string SexName { get; set; }
        public string Ignore { get; set; }
        public string Note { get; set; }
    }
}

在Application层新建Participant文件,Dto文件!在Dto文件中新建上面的两个Dto类,下面的两个cs文件是接口与实现类,在下面测试使用!

其中我将 GetParticipantDto.cs的定义与实体定义一致

AbpAutoMapper配置映射关系

在这里插入图片描述

cfg.CreateMap<Participant.Dto.AddParticipantDto, EntityModel.Participant>()
                //Dto映射实体前,Dto修改
                .BeforeMap((s, t) => s.Name = "BeforeMap-" + s.Name)
                //Dto映射实体后,实体修改
                .AfterMap((s, t) => t.Name = t.Name + "-AfterMap")
                //条件映射,针对目标字段
                .ForMember(dest => dest.Age, opt => opt.Condition(src => src.Age >= 10 && src.Age <= 110))
                //忽略映射,针对目标字段
                .ForMember(t => t.Ignore, opt => opt.Ignore())
                //字段名不一致映射
                .ForMember(t => t.Remark, opt => opt.MapFrom(_ => _.Note))
                //字段名、类型不一致处理映射
                .ForMember(t => t.Sex, opt => opt.MapFrom(_ => _.SexName == "Male" ? 1 : 2));
                //实体映射Dto
                cfg.CreateMap<EntityModel.Participant, Participant.Dto.GetParticipantDto>();
              

编写上面讲到的接口和实现类代码,测试

namespace ABPTest.Participant
{
    /// <summary>
    /// 
    /// </summary>
    public interface IParticipantAppService:IApplicationService
    {
        /// <summary>
        /// 新增参与者
        /// </summary>
        /// <param name="pa"></param>
        void AddParticipant(AddParticipantDto pa);
        /// <summary>
        /// 获取所有者信息
        /// </summary>
        /// <returns></returns>
        GetParticipantDto GetOneParticipant();

    }
}
namespace ABPTest.Participant
{
    //[AbpAuthorize]  这里先去掉abp的授权校验,方便测试
    public class ParticipantAppService : ABPTestAppServiceBase, IParticipantAppService
    {
        public IRepository<EntityModel.Participant, int> _participantRepository;
		//依赖注入
        public ParticipantAppService(IRepository<EntityModel.Participant, int> par)
        {
            _participantRepository = par;
        }

        public void AddParticipant(AddParticipantDto pa)
        {
            var p = ObjectMapper.Map<EntityModel.Participant>(pa);
            if (p == null)
                return;
            _participantRepository.Insert(p);
        }

        public GetParticipantDto GetOneParticipant()
        {
            var data = _participantRepository.GetAll().FirstOrDefault();

            var result = ObjectMapper.Map<GetParticipantDto>(data);

            return result;
        }
    }
}

接口与实现类必须已***AppService结尾

完成后,设置web项目为启动项启动!

post调用新增方法,post地址:http://localhost:端口/api/services/app/participant/AddParticipant
其中 /api/services/app是固定的abp动态api地址,后面的分别是 /Service名称/内部方法

post调用查询方法,post地址:http://localhost:端口/api/services/app/participant/GetOneParticipant

接下来就可以自己修改post对象内容测试

如果对api测试有疑问的,可以参考详细篇章: abp集成swaggerui调试api

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值