Solved

Duplicate Code Driving me Crazy

Posted on 2013-01-03
1
380 Views
Last Modified: 2013-01-07
Hello,

I am trying to improve my coding practices, and have an area I could use help getting some advice on.  This is the code in a static Class in a WCF project.  It checks to see if an item is in the database already and active, in the database and inactive, or not in the database. del in the database is a bit type so will return true or false.
 public static bool? CheckForComplaintCode(IndComplaint ind)
        {
            SqlConnection conn = new SqlConnection(ConnectionStrings.mainConn);
            string sql = "SELECT del FROM comcodes where comcode = @ComCode";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(new SqlParameter("@ComCode", ind.ComCode));
            bool? count;
            conn.Open();
            count = (bool?)cmd.ExecuteScalar();
            conn.Close();
            return count;
        }

        public static bool? CheckForDepartmentCode(IndDepartment ind)
        {
            SqlConnection conn = new SqlConnection(ConnectionStrings.mainConn);
            string sql = "SELECT del FROM deptcode where deptcode = @DeptCode";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(new SqlParameter("@DeptCode", ind.DeptCode));
            bool? count;
            conn.Open();
            count = (bool?)cmd.ExecuteScalar();
            conn.Close();
            return count;
        }//end def 'CheckForDepartmentCode'

Open in new window


I have 15 of these, each taking a differnt object.  I understand duplicated code is not ideal, and want to know another way of doing this, that uses some aspect of C# I am not fully educated in.

 I thought about using inheritance and making all the objects Derive from a Parent calss, then do the following:
 if (object is IndDepartment) { do stuff.. }
 but that seems like it will be the same amount of code, but only in one function.

I would like to use Delegates, Func<>  or Action<> to solve this, but while I understande the syntax, I cant wrap my head around how to apply them (of if they even apply in this situation).  

Any help would be appreciated.  Thank you.
0
Comment
Question by:paulpp
1 Comment
 
LVL 1

Accepted Solution

by:
ogm earned 500 total points
ID: 38742564
A simple solution would have a single CheckForCode function with 2 parameters: the code name, and the value to check for.

Something like this:

public static bool? CheckForCode(string codeName, int value)
        {
            SqlConnection conn = new SqlConnection(ConnectionStrings.mainConn);
            string sql = "SELECT del FROM " + codeName + " where " + codeName +" = @Value";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add(new SqlParameter("@Value", value));
            bool? count;
            conn.Open();
            count = (bool?)cmd.ExecuteScalar();
            conn.Close();
            return count;
        }//end def 'CheckForCode'

Open in new window


You can do a sanity check for the codeName argument if it's necessary, so the function only accepts valid ones.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

914 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

14 Experts available now in Live!

Get 1:1 Help Now