ValueInjecter@3.2.0 簡單用法

目錄
基本用法
-
一般對應
var mapper = Mapper.Map<TargetType>(customer);var mapper = Mapper.Map<FromType, TargetType>(customer);
-
客製化對應
1 2 3 4 5 6 7Mapper.AddMap<FromType, TargetType>(src => { var res = new TargetType(); res.InjectFrom(src); // 對應相同名字及型別的屬性 res.FullName = src.FirstName + " " + src.LastName; return res; }); -
InjectFrom的用法InjectFrom<TInjection>(source)用於使用約定映射,當未指定TInjection時,它將對應具有完全相同名稱和類型的屬性1 2 3 4target.InjectFrom(source); target.InjectFrom<Injection>(source); target.InjectFrom(new Injection(parameters), source); target.InjectFrom<Injection>();
專案上使用遇到的問題
-
當
List需要轉換對應時- 問題:
1 2 3 4 5 6public async Task<List<EmplyeeListDto>> GetAllEmployeeAsync(CancellationToken token) { var query = await _context.Emplyees.OrderByDescending(e => e.Jointime).ToListAsync(token); var data = Mapper.map<List<EmplyeeListDto>>(query);//此方式是無法對應任何屬性的 return data; }- 解法:
1 2 3 4 5 6public async Task<List<EmplyeeListDto>> GetAllEmployeeAsync(CancellationToken token) { var query = await _context.Employees.OrderByDescending(e => e.Jointime).ToListAsync(token); var data = query.Select(q => new EmplyeeListDto().InjectFrom(q)).Cast<EmplyeeListDto>().ToList();//改用此方式即可,需使用Cast<T>才會是正確的屬性 return data; }如沒使用
Cast<T>則會變成 Object 屬性: