.NET Core 中AutoMapper的配置及使用

預立科技發表於2019-11-14

一、AutoMapper說明

1.AutoMapper是一個物件-物件對映器。物件-物件對映通過將一種型別的輸入物件轉換為另一種型別的輸出物件來工作。

二、.NET Core中配置AutoMapper

1.NuGet安裝AutoMapper.Extensions.Microsoft.DependencyInjection

2.建立配置檔案,並新增對映配置
需要繼承AutoMapper中的Profile,

public class AutoMapperProfiles : Profile
{
    public AutoMapperProfiles()
    {
        //建構函式中建立對映關係
        CreateMap<UserCreateInput, User>();
    }
}

3.在Startup啟動類中的ConfigureServices方法中將服務新增到容器

AutoMapperProfiles是上面步驟中定義的配置檔案

services.AddAutoMapper(typeof(AutoMapperProfiles));

三、對映示例

1.簡單用法
(1) 定義Model

namespace Model.AutoMapper
{
    public class UserEntity
    {
        public Guid UserId { get; set; }
        public string UserName { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }
        public int IsActive { get; set; }
        public DateTime CreateDate { get; set; }
    }

    public class UserCreateInput
    {
        public string UserName { get; set; }
        public int Age { get; set; }
        public int Sex { get; set; }
    }
   
    public class UserCreateOutput
    {
        public Guid UserId { get; set; }
        public string UserName { get; set; }
        public int IsActive { get; set; }
    }
}

(2) 配置Profiles

public class AutoMapperProfiles : Profile
{
    //建構函式中建立對映關係
    public AutoMapperProfiles()
    {
        CreateMap<UserCreateInput, UserEntity>();
        CreateMap<UserEntity, UserCreateOutput>();
    }
}

說明:
CreateMap建立時是單項對映, 例如CreateMap<UserCreateInput, UserEntity>(),對映只能是從UserCreateInput對映成UserEntity;
如果想要兩個相互對映就必須CreateMap<UserCreateInput, UserEntity>().ReverseMap();

(3)模型對映

public class AutoMapperController : ControllerBase
{
    private IMapper mapper;
    public AutoMapperController(IMapper mapper)
    {
        //利用建構函式的依賴注入,注入物件IMapper
        this.mapper = mapper;
    }
    
    public UserCreateOutput Create()
    {
        //輸入引數UserCreateInput模擬
        UserCreateInput input = new UserCreateInput
        {
            UserName = "小張",
            Age = 20,
            Sex = 1
        };

        //對輸入引數UserCreateInput進行對映,對映成實體User
        UserEntity user = mapper.Map<UserEntity>(input);

        UserEntity restuleUser = UserEntityInsert(user);

        //對實體User進行對映,對映成輸出引數UserCreateOutput
        var output = mapper.Map<UserCreateOutput>(restuleUser);

        return output;
    }

    private UserEntity UserEntityInsert(UserEntity entity)
    {
        //模擬插入後返回
        entity.UserId = Guid.NewGuid();
        entity.IsActive = 1;
        entity.CreateDate = DateTime.Now;
        return entity;
    }
    
}

2.相互對映就必須CreateMap<UserCreateInput, UserEntity>().ReverseMap()

public class AutoMapperProfiles : Profile
{
    //建構函式中建立對映關係
    public AutoMapperProfiles()
    {
        CreateMap<UserCreateInput, UserEntity>().ReverseMap();
    }
}

public void Create1()
{
    //輸入引數UserCreateInput模擬
    UserCreateInput input = new UserCreateInput
    {
        UserName = "小張",
        Age = 20,
        Sex = 1
    };

    //對輸入引數UserCreateInput進行對映,對映成實體UserEntity
    UserEntity user = mapper.Map<UserEntity>(input);

    user.UserId = Guid.NewGuid();
    user.IsActive = 1;
    user.CreateDate = DateTime.Now;
    user.UserName = "小李";

    //對實體UserEntity進行對映,對映成輸出引數UserCreateInput
    var output = mapper.Map<UserCreateInput>(user);
}

四、異常示例:

1.Missing type map configuration or unsupported mapping. 因為沒有建立對映關係CreateMap<T1, T2>();

五、AutoMapper官網

AutoMapper官網:

http://automapper.org/

AutoMapper官網文件:

https://automapper.readthedocs.io/en/latest/

相關文章