Здравствуйте, я написал общий метод для отображения 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; }
}
Этот метод возвращает List<Company>
, в котором есть записи, затем я использую карту, чтобы получить 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;
}