Demo项目地址:https://download.youkuaiyun.com/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传输对象
- AddParticipantDto.cs
- 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