Fork me on GitHub

ASP NET Core ---Automapper

官方文档:http://docs.automapper.org/en/stable/index.html

 

一、安装和配置:

 

 二、使用:

         1、建立 Profile文件:

    public class MappingProfile:Profile
    {
        public MappingProfile()
        {
            CreateMap<Post, PostDTO>().ForMember(dest=>dest.Updatetime,opt=>opt.MapFrom(src=>src.LastModified));//字段映射
            CreateMap<PostDTO, Post>();
        }
    }

           2、controller 使用

        private readonly IMapper _mapper;

        public PostController(IPostRepository postRepository, IUnitOfWork unitOfWork,IMapper mapper)
        {
            _postRepository = postRepository;
            _unitOfWork = unitOfWork;
            _mapper = mapper;
        }
        [HttpGet]
        public async Task<IActionResult>  Get()
        {
            var posts = await _postRepository.GetPosts();
            var postDto=_mapper.Map<IEnumerable<Post>,IEnumerable<PostDTO>>(posts);
            return Ok(postDto);
        }

 

 

参考资料:https://v.qq.com/x/page/z0763a2x3j5.html

 

posted @ 2018-09-04 17:35  精进的小陈  阅读(373)  评论(0编辑  收藏  举报