Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Refactoring C# Code for Conversion

Posted on 2013-11-13
2
Medium Priority
?
179 Views
Last Modified: 2013-11-20
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

0
Comment
Question by:CoventryFirst
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
jasonduan earned 1500 total points
ID: 39651046
Replace line #11 -  35 with the following:

public static IQueryable<Pricing> ConvertToPricing(this IQueryable<PolicytblPricingModelPricingSavedPricing> query)
        {
            var newQuery = from q in query
                           select q.ConvertToPricing();

            return newQuery;
        }

        public static IEnumerable<Pricing> ConvertToPricing(this IEnumerable<PolicytblPricingModelPricingSavedPricing> inList)
        {
            var newList = from l in inList
                          select l.ConvertToPricing();

            return newList.AsEnumerable();
        }

Open in new window

0
 

Author Closing Comment

by:CoventryFirst
ID: 39663636
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.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question