Skip to content

Instantly share code, notes, and snippets.

@DrkWzrd
Created January 28, 2023 13:03
Show Gist options
  • Select an option

  • Save DrkWzrd/6daf945b36fa4a979214880c40fb80c4 to your computer and use it in GitHub Desktop.

Select an option

Save DrkWzrd/6daf945b36fa4a979214880c40fb80c4 to your computer and use it in GitHub Desktop.
Automapper tries to read a nullable value without the nullable guard '?'
public abstract record PersonDto : BaseDto
{
protected PersonDto() { }
public string Firstname { get; init; }
public string Lastname { get; init; }
public string FullName => $"{Firstname} {Lastname}";
public Gender Gender { get; init; }
public DateOnly Birthday { get; init; }
public IReadOnlyCollection<Guid> AddressesIds { get; init; }
public Guid? MainAddressId { get; init; }
public IReadOnlyCollection<Guid> EmailsIds { get; init; }
public Guid? MainEmailId { get; init; }
public IReadOnlyCollection<Guid> PhoneNumbersIds { get; init; }
public Guid? MainPhoneNumberId { get; init; }
public byte[]? ProfileImageData { get; init; }
}
internal abstract class Person : SoftDeletableEntity, IPerson
{
public Person()
{
}
[StringLength(100)]
public string Firstname { get; set; }
[StringLength(300)]
public string Lastname { get; set; }
public string FullName => $"{Firstname} {Lastname}";
public Gender Gender { get; set; }
public DateOnly Birthday { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual Address? MainAddress => Addresses.Where(adr => adr.IsPrimary).FirstOrDefault();
public virtual ICollection<Email> Emails { get; set; }
public virtual Email? MainEmail => Emails.Where(em => em.IsPrimary).FirstOrDefault();
public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
public virtual PhoneNumber? MainPhoneNumber => PhoneNumbers.Where(pn => pn.IsPrimary).FirstOrDefault();
public byte[]? ProfileImageData { get; set; }
IReadOnlyCollection<IAddress> IPerson.Addresses => (HashSet<Address>)Addresses;
IReadOnlyCollection<IEmail> IPerson.Emails => (HashSet<Email>)Emails;
IReadOnlyCollection<IPhoneNumber> IPerson.PhoneNumbers => (HashSet<PhoneNumber>)PhoneNumbers;
IAddress? IPerson.MainAddress => MainAddress;
IEmail? IPerson.MainEmail => MainEmail;
IPhoneNumber? IPerson.MainPhoneNumber => MainPhoneNumber;
}
public class PeopleProfile : Profile
{
public PeopleProfile()
{
CreateMap<BaseEntity, BaseDto>()
.Include<Person, PersonDto>()
.Include<PersonalInfo, PersonalInfoDto>();
CreateMap<Person, PersonDto>()
.Include<Patient, PatientDto>()
.Include<MustBeAdult, MustBeAdultDto>();
CreateMap<Patient, PatientDto>();
CreateMap<MustBeAdult, MustBeAdultDto>()
.Include<LegalGuardian, LegalGuardianDto>()
.Include<Employee, EmployeeDto>();
CreateMap<LegalGuardian, LegalGuardianDto>();
CreateMap<Employee, EmployeeDto>()
.Include<Doctor, DoctorDto>();
CreateMap<Doctor, DoctorDto>();
CreateMap<PersonalInfo, PersonalInfoDto>()
.Include<Address, AddressDto>()
.Include<PhoneNumber, PhoneNumberDto>()
.Include<Email, EmailDto>();
CreateMap<Address, AddressDto>();
CreateMap<PhoneNumber, PhoneNumberDto>();
CreateMap<Email, EmailDto>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment