其他分享
首页 > 其他分享> > ABP vNext V5 + VS2022+ .Net 6.0 学习笔记(4)--AutoMapper, FullAuditedEntity

ABP vNext V5 + VS2022+ .Net 6.0 学习笔记(4)--AutoMapper, FullAuditedEntity

作者:互联网

在第1篇用的ToDoItem 继承的是BasicAggregateRoot, 但实际上企业应用,可能大部分实体,需要软删除,记录创建/修改/删除的人和时间. 来满足审计的要求

 这就要用到FullAuditedEntity, 

 

 

Dto和Entity要映射,如果没有映射会出现以下错误

2021-11-18 13:32:08.145 +08:00 [INF] Route matched with {action = "GetList", controller = "Announcement", area = "", page = ""}. Executing controller action with signature System.Threading.Tasks.Task`1[System.Collections.Generic.List`1[MetaBase.Platform.AnnouncementDto]] GetListAsync() on controller MetaBase.Platform.AnnouncementAppService (MetaBase.Platform.Application).
2021-11-18 13:32:08.152 +08:00 [ERR] ---------- RemoteServiceErrorInfo ----------
{
  "code": null,
  "message": "An internal error occurred during your request!",
  "details": null,
  "data": {},
  "validationErrors": null
}

2021-11-18 13:32:08.152 +08:00 [ERR] Error mapping types.

Mapping types:
Object -> List`1
System.Object -> System.Collections.Generic.List`1[[MetaBase.Platform.AnnouncementDto, MetaBase.Platform.Application.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types:
Object -> List`1
System.Object -> System.Collections.Generic.List`1[[MetaBase.Platform.AnnouncementDto, MetaBase.Platform.Application.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
 ---> AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
Announcement -> AnnouncementDto
MetaBase.Platform.Announcement -> MetaBase.Platform.AnnouncementDto
   at lambda_method3301(Closure , Announcement , AnnouncementDto , ResolutionContext )
   at lambda_method3300(Closure , Object , List`1 , ResolutionContext )
   --- End of inner exception stack trace ---
   at lambda_method3300(Closure , Object , List`1 , ResolutionContext )
   at Volo.Abp.ObjectMapping.DefaultObjectMapper.Map[TSource,TDestination](TSource source) in D:\ci\Jenkins\workspace\abp-framework-release\abp\framework\src\Volo.Abp.ObjectMapping\Volo\Abp\ObjectMapping\DefaultObjectMapper.cs:line 72
   at MetaBase.Platform.AnnouncementAppService.GetListAsync() i

 在Application项目的XXXApplicationAutoMapperProfile里添加Map, 重新生成HttpApi.Host 项目

CreateMap<AnnouncementDto, Announcement>().IgnoreAuditedObjectProperties();  //IgnoreAuditedObjectProperties 好像不加也可以
CreateMap<Announcement, AnnouncementDto>(); //加这句能从实体=>Dto
实体<=>Dto 这样要写2句也太麻烦了. 改成这样
 CreateMap<Announcement, AnnouncementDto>().ReverseMap(); //双向映射

假如Dto和Entity的字段名,不一样, 假如我把Dto改成这样

 public class AnnouncementDto
 {
        public Guid Id { get; set; }  //Entity里默认用Id做主键,Dto则要显式指定guid
        public String Subject { get; set; }  //标题Subject对应Entity.Title
        public String Content { get; set; } //公告内容对应Entity.ContentDesc
        public String ColumnCode { get; set; } //栏目编码
}

 

对应的Mapping就要这样写

            CreateMap<Announcement, AnnouncementDto>()
                .ForMember(x=>x.Subject,options=>options.MapFrom(input=>input.Title))
                .ForMember(x => x.Content, options => options.MapFrom(input => input.ContentDesc))
                .ReverseMap();

在Application项目的实现方法里就可以用ObjectMapper.Map的方法来转换

  public async Task<AnnouncementDto> GetAsync(Guid id)
        {
            var entity =await _Repo.GetAsync(id);
            return ObjectMapper.Map<Announcement, AnnouncementDto>(entity);
   
        }


        public async Task<List<AnnouncementDto>> GetListAsync()
        {
            var items = await _Repo.GetListAsync();
            //entity list=> Dto list
            return ObjectMapper.Map<List<Announcement>, List<AnnouncementDto>>(items);

        }

 WebAPI 方法命名的约定, Create/Get/Update/Delete ==> Post/Get/Put/Delete

我尝试加几个接口方法,  GetAllListAysnc=> /List  去掉了All  , GetAnyList2Async =-> any-list2

 

 

 

 



标签:vNext,MetaBase,Dto,--,FullAuditedEntity,List,Platform,AnnouncementDto,public
来源: https://www.cnblogs.com/zitjubiz/p/abp_net6_vs2022_4.html