skip to Main Content

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


  1. Chosen as BEST ANSWER

    hi for above i found that i can use dynamic object

    request.Policy = PolicyRequest.MapRemainings(request);
    
    public dynamic MapRemainings(dynamic request)
        {
            var 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(),
            };
    return policy;
     }
    

  2. You can create a common method to map the remaining properties between PolicyRequest and Policy. To do this, you can create a static utility method that takes a PolicyRequest object and returns a Policy object with the mapped properties. Here’s an example of how you can achieve this:

    public class PolicyRequest
    {
        // ... Other properties ...
    
        public Policy MapToPolicy()
        {
            return new Policy
            {
                Title = Title,
                Forename = Forename,
                Surname = Surname,
                DateOfBirth = DateOfBirth,
                Landline = Landline,
                Mobile = Mobile,
                Email = Email,
                Address1 = (Address1 ?? string.Empty).Trim(),
                Address2 = (Address2 ?? string.Empty).Trim(),
                Address3 = (Address3 ?? string.Empty).Trim(),
                Address4 = (Address4 ?? string.Empty).Trim()
            };
        }
    }
    
    public class Policy
    {
        // ... Properties ...
    }
    
    // Usage
    PolicyRequest request = new PolicyRequest();
    Policy policy = request.MapToPolicy();
    

    With this approach, you can call the MapToPolicy method to map the properties from PolicyRequest to Policy. 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:

    foreach (var policy in policies)
    {
        policy.PreviousReference = GetPreviousReference(); // You need to implement this method
        policy.Policy = policy.MapToPolicy();
    }
    

    This way, you have a clean and reusable mapping method, and you can set the PreviousReference as needed for each policy object.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search