Solved

Refactoring C# Code for Conversion

Posted on 2013-11-13
2
173 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 500 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now