Avatar of Cov IT
Cov IT
 asked on

Refactoring C# Code for Conversion

I have the class below and I'd like to factor out the common piece (the conversion of a DAL PolicytblPricingModelPricingSavedPricing object to a domain Pricing object). Specifically, this...

select new Pricing
{
   PricingModelID = q.PricingModelID,
   PolicyFaceAmount = q.FaceAmt.HasValue ? q.FaceAmt.Value : 0
}

Open in new window


Can someone provide some guidance on how to accomplish this? Is it possible? I am having difficulty figuring out how to extract this piece.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PM.DAL;
using PM.Common.Domain;
using System.Linq.Expressions;

namespace PM.BusinessLogic.Converters
{
    public static class TblSavedPricingConverter
    {
        public static IQueryable<Pricing> ConvertToPricing(this IQueryable<PolicytblPricingModelPricingSavedPricing> query)
        {
            var newQuery = from q in query
                           select new Pricing
                           {
                               PricingModelID = q.PricingModelID,
                               PolicyFaceAmount = q.FaceAmt.HasValue ? q.FaceAmt.Value : 0
                           };

            return newQuery;
        }

        public static IEnumerable<Pricing> ConvertToPricing(this IEnumerable<PolicytblPricingModelPricingSavedPricing> inList)
        {
            var newList = from l in inList
                           select new Pricing
                           {
                               PricingModelID = l.PricingModelID,
                               PolicyFaceAmount = l.FaceAmt.HasValue ? l.FaceAmt.Value : 0
                           };

            return newList.AsEnumerable();
        }

        public static Pricing ConvertToPricing(this PolicytblPricingModelPricingSavedPricing original)
        {
            if (original == null)
                return new Pricing();

            var newObject = new Pricing()
            {
                PricingModelID = original.PricingModelID,
                PolicyFaceAmount = original.FaceAmt.HasValue ? original.FaceAmt.Value : 0
            };

            return newObject;
        }
    }
}

Open in new window

Microsoft Development

Avatar of undefined
Last Comment
Cov IT

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jasonduan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Cov IT

ASKER
Thanks. That does do what we'd like to do. As a side note, we did switch to using Auto Mapper to take care of our mapping and that also abstracted out this particular piece.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy