8.AutoMapper

作者: 落地成佛 | 来源:发表于2017-08-28 13:52 被阅读19次

一、概述

AutoMapper是用于DTO对象与实体之间的映射,有两种方式:
第一种:使用IObjectMapper接口
第二种:特性注入

二、使用

2.1 使用IObjectMapper接口

public class UserAppService : ApplicationService
{
    private readonly IObjectMapper _objectMapper;
    public UserAppService(IObjectMapper objectMapper)
    {
          _objectMapper = objectMapper;
    } 
    public void CreateUser(CreateUserInput input)
    {
          var user = _objectMapper.Map<User>(input);
    }
}

2.2特性注入

[AutoMapTo(typeof(User))]
public class CreateUserInput
{
public string Name { get; set; }
public string Surname { get; set; }
public string EmailAddress { get; set; }
public string Password { get; set; }
}

相关文章

  • 8.AutoMapper

    一、概述 AutoMapper是用于DTO对象与实体之间的映射,有两种方式:第一种:使用IObjectMapper...

网友评论

    本文标题:8.AutoMapper

    本文链接:https://www.haomeiwen.com/subject/xgnwdxtx.html