Automapper 不会将 T 的通用集合映射到 TD 的通用集合
Automapper doesn't map Generic collection of T to Generic collection of TD
您好,我写了一个将 DTO 映射到 POCO 的通用方法,反之亦然。但是,当我传递 POCO 列表时,它不会将其映射到 DTO 列表。看下面:
public static class BaseMapper<T, TD>
where T : class
where TD : class
{
public static List<TD> Map(List<T> entity)
{
var config = new MapperConfiguration(cfg => cfg.CreateMap<List<T>, List<TD>>());
var mapper = config.CreateMapper();
return mapper.Map<List<TD>>(entity);
}
public static List<T> Map(List<TD> dto)
{
var config = new MapperConfiguration(cfg => cfg.CreateMap<List<TD>, List<T>>());
var mapper = config.CreateMapper();
return mapper.Map<List<T>>(dto);
}
}
这是我的 POCO:
public partial class Company : Core
{
public string Title { get; set; }
}
/// <summary>
/// Company Virtual Properties
/// </summary>
public partial class Company
{
public virtual ICollection<Cartable> Cartables { get; } = new HashSet<Cartable>();
}
这是我的 DTO
public class CompanyDTO : CoreDTO
{
public string Title { get; set; }
}
此方法 returns List<Company>
有记录,然后我使用 map 获取 List<CompanyDTO>
然后它为空。
using static BaseMapper<Company, CompanyDTO>;
public Response<List<CompanyDTO>> GetAll(Guid currentUserId, int pageNum, int pageSize)
{
var ProcessResponse = new Response<List<CompanyDTO>>();
try
{
var entities = _unitOfWork.Companies.Query().OrderBy(x => x.Title).Skip(pageNum * pageSize).Take(pageSize).ToList();
ProcessResponse.Result = Map(entities); //it turns out null here.
ProcessResponse.RecCount = _unitOfWork.Companies.Query().Count();
}
catch (Exception ex)
{
ProcessResponse.Failed(ex.Message, ex.InnerException.Message);
}
return ProcessResponse;
}
您需要为实体类型而不是集合类型进行映射。
即
new MapperConfiguration(cfg => cfg.CreateMap<TD, T>());
而不是
new MapperConfiguration(cfg => cfg.CreateMap<List<TD>, List<T>>());
然后就可以了
P.S。 Automapper 有非常好的文档——你没有尝试阅读吗? https://docs.automapper.org/en/stable/Lists-and-arrays.html
您好,我写了一个将 DTO 映射到 POCO 的通用方法,反之亦然。但是,当我传递 POCO 列表时,它不会将其映射到 DTO 列表。看下面:
public static class BaseMapper<T, TD>
where T : class
where TD : class
{
public static List<TD> Map(List<T> entity)
{
var config = new MapperConfiguration(cfg => cfg.CreateMap<List<T>, List<TD>>());
var mapper = config.CreateMapper();
return mapper.Map<List<TD>>(entity);
}
public static List<T> Map(List<TD> dto)
{
var config = new MapperConfiguration(cfg => cfg.CreateMap<List<TD>, List<T>>());
var mapper = config.CreateMapper();
return mapper.Map<List<T>>(dto);
}
}
这是我的 POCO:
public partial class Company : Core
{
public string Title { get; set; }
}
/// <summary>
/// Company Virtual Properties
/// </summary>
public partial class Company
{
public virtual ICollection<Cartable> Cartables { get; } = new HashSet<Cartable>();
}
这是我的 DTO
public class CompanyDTO : CoreDTO
{
public string Title { get; set; }
}
此方法 returns List<Company>
有记录,然后我使用 map 获取 List<CompanyDTO>
然后它为空。
using static BaseMapper<Company, CompanyDTO>;
public Response<List<CompanyDTO>> GetAll(Guid currentUserId, int pageNum, int pageSize)
{
var ProcessResponse = new Response<List<CompanyDTO>>();
try
{
var entities = _unitOfWork.Companies.Query().OrderBy(x => x.Title).Skip(pageNum * pageSize).Take(pageSize).ToList();
ProcessResponse.Result = Map(entities); //it turns out null here.
ProcessResponse.RecCount = _unitOfWork.Companies.Query().Count();
}
catch (Exception ex)
{
ProcessResponse.Failed(ex.Message, ex.InnerException.Message);
}
return ProcessResponse;
}
您需要为实体类型而不是集合类型进行映射。 即
new MapperConfiguration(cfg => cfg.CreateMap<TD, T>());
而不是
new MapperConfiguration(cfg => cfg.CreateMap<List<TD>, List<T>>());
然后就可以了
P.S。 Automapper 有非常好的文档——你没有尝试阅读吗? https://docs.automapper.org/en/stable/Lists-and-arrays.html