Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Refactoring C# Code for Conversion

Posted on 2013-11-13
2
Medium Priority
?
180 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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 a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

971 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