User-309523270 posted
Hi,
I have this model:
public class PartyDTO
{
public int? PartyId { get; set; }
public List<AddressEntryDTO> AddressList { get; set; }
public List<PartyAliasDTO> PartyAliasList { get; set; }
}
public class PartyEntry : PartyDTO
{
public int? PartyId { get; set; }
}
public class PersonEntry : PartyEntry
{
public int? PersonId { get; set; }
public string CII { get; set; }
public PersonAliasDTO personAliasDTO { get; set; }
}
public class PartyAliasDTO
{
public int? PartyAliasId { get; set; }
public int? AliasSeq { get; set; }
}
public class PersonAliasDTO : PartyAliasDTO
{
public int? personId { get; set; }
public int? partyAliasId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string SuffixName { get; set; }
public DateTime BirthDate { get; set; }
public string DriversLicense { get; set; }
public string DriversLicenseState { get; set; }
public string SSN { get; set; }
}
I have the script below to insert a person. Please mind the comments in
italics.
Web Application
==================
PersonController.cs
----------------------
[HttpPost]
public async Task<IActionResult> Create(PersonEntry oNewPersonEntry)
{
var resp = await _partyClient.Create(oNewPersonEntry);
/* oNewPersonEntry captured correctly from the view for PersonEntry and 1 PersonAliasDTO record */
}
PartyClient.cs
------------------------
public class PartyClient
{
private readonly HttpClient httpClient;
public PartyClient()
{
httpClient = new HttpClient() { BaseAddress = new Uri("http://localhost:65262/api/party/") };
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<PartyEntry> Create(PartyEntry p)
{
/****** p captures correctly, with PersonEntry and 1 PersonAliasDTO record *******/
var resp = await httpClient.PostAsync($"",
new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(p).ToString(), Encoding.UTF8, "application/json"));
if (resp.IsSuccessStatusCode)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<PartyEntry>(await resp.Content.ReadAsStringAsync());
}
else
{
return null;
}
}
API-Controller
===================
PartyController.cs
--------------------
public class PartyController : Controller
{
private readonly PartyRepository _partyRepository;
private readonly PartyCategoryRepository _partyCategoryRepository;
private readonly UWPerson _uwPerson;
private readonly IMapper _mapper;
public PartyController(IUWPerson _vUWPerson, PartyRepository _vPartyRepository, PersonRepository _vPersonRepository, IMapper mapper)
{
_uwPerson = (UWPerson) _vUWPerson;
_partyRepository = _vPartyRepository;
_personRepository = _vPersonRepository;
_mapper = mapper;
}
[HttpPost]
public async Task<IActionResult> CreateUW([FromBody]PartyEntry oParty)
{
/* over here oParty captures the fields PartyEntry and PartyDTO fields
public class PartyDTO
{
int? PartyId { get; set; }
List<AddressEntryDTO> AddressList { get; set; }
List<PartyAliasDTO> PartyAliasList { get; set; }
}
* Question 1: where are my PersonEntry fields CII and 1 personAliasDTO
?
*/
PersonEntry per = (PersonEntry) oParty; /* Question 2: if i do this, it casts an invalid cast exception. can't i upcast a person into its base class when I call a method and downcast in the body of the method? */
PartyEntry res = await _uwPerson.Create(oParty);
}
Thanks for your help!
Best,
tinac99