Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Duplicate Code Driving me Crazy

Posted on 2013-01-03
1
Medium Priority
?
394 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
[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
1 Comment
 
LVL 1

Accepted Solution

by:
ogm earned 2000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 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