Code duplication, same functionality different types

jankes

I have this method that works with Npgsql:

private DataTable GetTableN(string sql, string[] pars)
    {
        NpgsqlCommand zapytanie = new NpgsqlCommand(sql, connn, trann);
        NpgsqlDataAdapter da = new NpgsqlDataAdapter();
        DataSet ds = new DataSet();
        try
        {
            if (pars != null)
            {
                for (int i = 0; i < pars.Length; i++)
                {
                    zapytanie.Parameters.AddWithValue("@param" + i, pars[i]);
                }
            }
            connn.Open();
            da.SelectCommand = zapytanie;
            da.Fill(ds);
            return ds.Tables[0];
        }
        catch (NpgsqlException e)
        {
            throw (new SqlException(e.Message.ToString()));
        }
        finally
        {
            connn.Close();
            zapytanie.Dispose();
            da.Dispose();
            ds.Dispose();
        }
    }

now I need to have exactly the same method but using Odbc instead. I'd only need to make these changes

  1. NpgsqlCommand to ObdcCommand
  2. NpgsqlDataAdapter to OdbcDataAdapter
  3. NpgsqlException to OdbcException

How do I merge this in order to avoid code duplication and have only one method ?

tym32167

you can try to change signature of your method

private DataTable GetTableN(string sql, string[] pars, DbCommand zapytanie, DbDataAdapter da)
{   
    DataSet ds = new DataSet();
    try
    {
        if (pars != null)
        {
            for (int i = 0; i < pars.Length; i++)
            {
                zapytanie.Parameters.AddWithValue("@param" + i, pars[i]);
            }
        }
        connn.Open();
        da.SelectCommand = zapytanie;
        da.Fill(ds);
        return ds.Tables[0];
    }
    catch (DbException e)
    {
        throw (new SqlException(e.Message.ToString()));
    }
    finally
    {
        connn.Close();
        zapytanie.Dispose();
        da.Dispose();
        ds.Dispose();
    }
}

or use some kind of factory

private DataTable GetTableN(string sql, string[] pars, MyFactory factory)
{
    DbCommand zapytanie = factory.CreateCommand(...);
    DbDataAdapter da = new factory.CreateAdapter(...);
    ...
}

or you able to use inheritance for that

public abstract class MyClass
{
    private DataTable GetTableN(string sql, string[] pars)
    {
        DbCommand zapytanie = CreateCommand();
        DbDataAdapter da = CreateAdapter();
        ...
    }

    protected abstract DbCommand CreateCommand();
    protected abstract DbDataAdapter CreateAdapter();
}

public class OdbcClass : MyClass
{
    protected override DbCommand CreateCommand()
    {
        // create for odbc
    }

    protected override DbDataAdapter CreateAdapter()
    {
        // create for odbc
    }
}

public class PostgrClass : MyClass
{
    protected override DbCommand CreateCommand()
    {
        // create for postgr
    }

    protected override DbDataAdapter CreateAdapter()
    {
        // create for postgr
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Removing code duplication in methods that looks the same but works with different types

From Dev

Multiple methods with same functionality but different types

From Java

How to avoid duplication of code regarding primitive types?

From Dev

How to avoid duplication of code regarding primitive types?

From Dev

Different types of the same object

From Dev

Same view controller UI, different functionality

From Dev

More classes with same static functions with different functionality

From Dev

Jquery same functionality for different class selectors

From Java

How to reduce code duplication when dealing with recursive sum types

From Dev

Clojure- Amending code for different functionality

From Dev

Different input types in the same method

From Dev

XSD different types same name

From Dev

Register same class for different types

From Dev

Symfony2 - Different services, same arguments - how to avoid duplication

From Dev

Code duplication or not?

From Dev

Code duplication or not?

From Dev

Different different version of same code

From Dev

Same template, different code?

From Dev

How to implement two inits with same content without code duplication in Swift?

From Dev

Preventing code duplication in procedurally similar functions that have different output?

From Dev

code duplication except different class is new-ed

From Java

How can I avoid duplicate code with the same method body but different return types in Java?

From Dev

Multiple Clients all requiring some different functionality within the same method

From Dev

Two separate click event having different functionality for same button?

From Dev

Haskell: Using the same operator on different types in a function

From Dev

JavaFX - Different css classes for same node types

From Dev

Android two different SharedPreference types with the same ID

From Dev

Interface with members with same name and different return types

From Dev

Gson - Same field name, different types

Related Related

  1. 1

    Removing code duplication in methods that looks the same but works with different types

  2. 2

    Multiple methods with same functionality but different types

  3. 3

    How to avoid duplication of code regarding primitive types?

  4. 4

    How to avoid duplication of code regarding primitive types?

  5. 5

    Different types of the same object

  6. 6

    Same view controller UI, different functionality

  7. 7

    More classes with same static functions with different functionality

  8. 8

    Jquery same functionality for different class selectors

  9. 9

    How to reduce code duplication when dealing with recursive sum types

  10. 10

    Clojure- Amending code for different functionality

  11. 11

    Different input types in the same method

  12. 12

    XSD different types same name

  13. 13

    Register same class for different types

  14. 14

    Symfony2 - Different services, same arguments - how to avoid duplication

  15. 15

    Code duplication or not?

  16. 16

    Code duplication or not?

  17. 17

    Different different version of same code

  18. 18

    Same template, different code?

  19. 19

    How to implement two inits with same content without code duplication in Swift?

  20. 20

    Preventing code duplication in procedurally similar functions that have different output?

  21. 21

    code duplication except different class is new-ed

  22. 22

    How can I avoid duplicate code with the same method body but different return types in Java?

  23. 23

    Multiple Clients all requiring some different functionality within the same method

  24. 24

    Two separate click event having different functionality for same button?

  25. 25

    Haskell: Using the same operator on different types in a function

  26. 26

    JavaFX - Different css classes for same node types

  27. 27

    Android two different SharedPreference types with the same ID

  28. 28

    Interface with members with same name and different return types

  29. 29

    Gson - Same field name, different types

HotTag

Archive