I am working on a task where i am making a database call and getting data back, and then i am mapping it to a class object.
public class PolicyRequest
{
public string Title { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public DateTime DateOfBirth { get; set; }
public string Landline { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string Address4 { get; set; }
public string Postcode { get; set; }
public string AccountName { get; set; }
public string AccountNumber { get; set; }
public string SortCode { get; set; }
public int PaymentDay { get; set; }
public string Reference { get; set; }
public Policy Policy { get; set; }
public string PreviousReference { get; set; }
}
public class Policy
{
public string Title { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public DateTime DateOfBirth { get; set; }
public string Landline { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string Address4 { get; set; }
public string Postcode { get; set; }
public string AccountName { get; set; }
public string AccountNumber { get; set; }
}
I am mapping whatever data i am getting back from db to the above class with automapper in that the policy object and Previousreference
is not mapped so i need to map it after i made the initial mapping. i can do it by declaring a new method, but i don’t need a repetitive code for each class as this is needed on multiple places. i need a help that how i can make a common method which maps these remaining data.
request.PreviousReference = request.PreviousReference;
request.Policy = new Policy
{
Title = request.Title,
Forename = request.Forename,
Surname = request.Surname,
DateOfBirth = request.DateOfBirth,
Landline = request.Landline,
Mobile = request.Mobile,
Email = request.Email,
Address1 = (request.Address1 ?? string.Empty).Trim(),
Address2 = (request.Address2 ?? string.Empty).Trim(),
Address3 = (request.Address3 ?? string.Empty).Trim(),
Address4 = (request.Address4 ?? string.Empty).Trim(),
}
For e.g.
Can create a method called MapRemainings
which will take the policy object and map it to the remaining policy object and previous reference, but then i can’t make it a common method.
foreach (var policy in policies)
{
MapRemainings(policy);
}
public void MapRemainings(PolicyRequest request, Policy request, string previousReference)
{
request.Address1 = policyRequest.Address1;
}
Also, should I keep the method as void
Can anybody help me
2
Answers
hi for above i found that i can use dynamic object
You can create a common method to map the remaining properties between
PolicyRequest
andPolicy
. To do this, you can create a static utility method that takes aPolicyRequest
object and returns aPolicy
object with the mapped properties. Here’s an example of how you can achieve this:With this approach, you can call the
MapToPolicy
method to map the properties fromPolicyRequest
toPolicy
. This reduces code duplication and keeps your mapping logic within the respective classes.If you also want to set the
PreviousReference
property, you can do it separately in your loop:This way, you have a clean and reusable mapping method, and you can set the
PreviousReference
as needed for each policy object.