Я пытаюсь преобразовать тип сущности List<T>
в тип dto List<T>
, но попытка сделать это вызывает исключение system.stackoverflow.У меня есть два профиля сопоставления, один на бизнес-уровне, который отвечает за отображение типа объекта на тип dto, а другой на уровне представления, который отвечает за отображение dtos для просмотра моделей.Я зарегистрировал все свои профили, и эти сопоставления работают во многих других местах, но это не так.
Тип сущности
public partial class CompanyProfile
{
public CompanyProfile()
{
CompanyAddress = new HashSet<CompanyAddress>();
CompanyEmail = new HashSet<CompanyEmail>();
CompanyTelephone = new HashSet<CompanyTelephone>();
InsurancePolicy = new HashSet<InsurancePolicy>();
WarrantyDetail = new HashSet<WarrantyDetail>();
}
public Guid Id { get; set; }
public Guid BusinessTypeId { get; set; }
public string Name { get; set; }
public string Registration { get; set; }
public string Description { get; set; }
public bool IsDeleted { get; set; }
public string InsertDate { get; set; }
public virtual BusinessType BusinessType { get; set; }
public virtual ICollection<CompanyAddress> CompanyAddress { get; set; }
public virtual ICollection<CompanyEmail> CompanyEmail { get; set; }
public virtual ICollection<CompanyTelephone> CompanyTelephone { get; set; }
public virtual ICollection<InsurancePolicy> InsurancePolicy { get; set; }
public virtual ICollection<WarrantyDetail> WarrantyDetail { get; set; }
}
Тип DTO
public class CompanyProfileDTO
{
public Guid RecordPK { get; set; }
public Guid BusinessTypeFK { get; set; }
public string CompanyName { get; set; }
public string RegistrationNumber { get; set; }
public string CompanyDescription { get; set; }
public bool IsRemoved { get; set; }
public string CreationDate { get; set; }
public virtual BusinessTypeDTO BusinessTypeRecord { get; set; }
public virtual ICollection<CompanyAddressDTO> CompanyAddressList { get; set; }
public virtual ICollection<CompanyEmailDTO> CompanyEmailList { get; set; }
public virtual ICollection<CompanyTelephoneDTO> CompanyPhoneList { get; set; }
public virtual ICollection<InsurancePolicyDTO> InsurancePolicyList { get; set; }
public virtual ICollection<WarrantyDetailDTO> WarrantyDetailList { get; set; }
}
Просмотр модели Тип
[BindRequired]
public abstract class BaseCompanyVM
{
[HiddenInput, ReadOnly(true),
BindingBehavior(BindingBehavior.Optional)]
public Guid RecordPK { get; set; }
[Required(ErrorMessage = "Business type is required!"), Display(Name = "Business Type")]
public Guid BusinessTypeFK { get; set; }
[Required(ErrorMessage = "Company name is required!"), Display(Name = "Company Name"),
StringLength(100, ErrorMessage = "Company name must be at least {2} and at max {1} characters
long.", MinimumLength = 2)]
public string CompanyName { get; set; }
[Required(ErrorMessage = "Company registration number is required!"), Display(Name = "Company
Registration Number"),
StringLength(50, ErrorMessage = "Company registration number must be at least {2} and at max {1}
characters long.", MinimumLength = 2)]
public string RegistrationNumber { get; set; }
[Required(ErrorMessage = "Company description is required!"), Display(Name = "Company
Description"), DataType(DataType.MultilineText),
StringLength(450, ErrorMessage = "Company description must be at least {2} and at max {1}
characters long.", MinimumLength = 2)]
public string CompanyDescription { get; set; }
[BindingBehavior(BindingBehavior.Optional)]
public bool IsRemoved { get; set; } = false;
[HiddenInput, ReadOnly(true),
BindingBehavior(BindingBehavior.Optional)]
public string CreationDate { get; set; } = DateTime.Now.ToStringDateTime();
}
[BindRequired]
public class CompanyProfileVM : BaseCompanyVM
{
[BindingBehavior(BindingBehavior.Never)]
public virtual BusinessTypeVM BusinessTypeRecord { get; set; }
[BindingBehavior(BindingBehavior.Never)]
public virtual ICollection<CompanyAddressVM> CompanyAddressList { get; set; }
[BindingBehavior(BindingBehavior.Never)]
public virtual ICollection<CompanyEmailVM> CompanyEmailList { get; set; }
[BindingBehavior(BindingBehavior.Never)]
public virtual ICollection<CompanyTelephoneVM> CompanyPhoneList { get; set; }
[BindingBehavior(BindingBehavior.Never)]
public virtual ICollection<InsurancePolicyVM> InsurancePolicyList { get; set; }
[BindingBehavior(BindingBehavior.Never)]
public virtual ICollection<WarrantyDetailVM> WarrantyDetailList { get; set; }
}
Код уровня доступа к данным
public async Task<ResponseHelper> GetAllAsync()
{
List<CompanyProfile> recordList = new List<CompanyProfile>();
try
{
/*recordList = await context.CompanyProfile
.Include(i => i.BusinessType)
.Include(i => i.CompanyTelephone)
.ThenInclude(i => i.ContactType)
.ThenInclude(i => i.CompanyTelephone)
.ThenInclude(i => i.Telephone)
.Include(i => i.CompanyAddress)
.ThenInclude(i => i.AddressType)
.ThenInclude(i => i.CompanyAddress)
.ThenInclude(i => i.Address)
.ThenInclude(i => i.Province)
.Include(i => i.CompanyEmail)
.ThenInclude(i => i.ContactType)
.ThenInclude(i => i.CompanyEmail)
.ThenInclude(i => i.Email)
.OrderBy(o => o.Name)
.ToListAsync();*/
recordList = await context.CompanyProfile
.Include(i => i.BusinessType)
.Include(i => i.CompanyEmail)
.Include(i => i.CompanyAddress)
.Include(i => i.CompanyTelephone)
.OrderBy(o => o.Name)
.ToListAsync();
Message.Success = string.Format(EnumDescription
.GetEnumDescription(ResultCode.ReadAllMessage),
nameof(CompanyProfile).InsertSpace());
ResultCode = ResultCode.Success;
}
catch (Exception e)
{
return ResponseHelper.ExceptionResponse(nameof(CompanyProfileRepository),
nameof(CompanyProfileRepository.GetAllAsync), e);
}
return ResponseHelper.CreateResponse(ResultCode, Message, recordList);
}
Код уровня бизнес-логики , здесь происходит сбой сопоставления и возникает исключение
public async Task<ResponseHelper> GetAllAsync()
{
try
{
Response = await context.GetAllAsync();
Response.Data = mapper.Map<List<CompanyProfileDTO>>(Response?.Data);
if (Response.Message.Exception != null)
{
await errorContext.CreateAsync(Response.Message.Exception);
}
logger.LogActivity(Response);
}
catch (Exception e)
{
return ResponseHelper.ExceptionResponse(nameof(CompanyProfileLogic), nameof(CompanyProfileLogic.GetAllAsync), e);
}
return Response;
}
Отображение уровня бизнес-логики
CreateMap<CompanyProfile, CompanyProfileDTO>()
.ForMember(dest => dest.RecordPK, act => act.MapFrom(src => src.Id))
.ForMember(dest => dest.CompanyName, act => act.MapFrom(src => src.Name))
.ForMember(dest => dest.IsRemoved, act => act.MapFrom(src => src.IsDeleted))
.ForMember(dest => dest.CreationDate, act => act.MapFrom(src => src.InsertDate))
.ForMember(dest => dest.BusinessTypeFK, act => act.MapFrom(src => src.BusinessTypeId))
.ForMember(dest => dest.CompanyEmailList, act => act.MapFrom(src => src.CompanyEmail))
.ForMember(dest => dest.CompanyDescription, act => act.MapFrom(src => src.Description))
.ForMember(dest => dest.BusinessTypeRecord, act => act.MapFrom(src => src.BusinessType))
.ForMember(dest => dest.RegistrationNumber, act => act.MapFrom(src => src.Registration))
.ForMember(dest => dest.WarrantyDetailList, act => act.MapFrom(src => src.WarrantyDetail))
.ForMember(dest => dest.CompanyAddressList, act => act.MapFrom(src => src.CompanyAddress))
.ForMember(dest => dest.CompanyPhoneList, act => act.MapFrom(src => src.CompanyTelephone))
.ForMember(dest => dest.InsurancePolicyList, act => act.MapFrom(src => src.InsurancePolicy))
.ReverseMap();
Отображение слоя представления
CreateMap<CompanyProfileVM, CompanyProfileDTO>().ReverseMap();