Solved

Duplicate Code Driving me Crazy

Posted on 2013-01-03
1
383 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Media.Imaging 1 18
List<PaisEntity> - show some 1 24
Expression Evaluater 3 26
VB.NET 2008 Winforms Signing 13 19
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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