Solved

Refactoring C# Code for Conversion

Posted on 2013-11-13
2
178 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 500 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

622 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