David Dury
David Dury

Reputation: 5717

Reduce code duplicates

Currently I have quite many classes (5) that have just 2 properties but have different names for different purposes:

public class Class1
{
    public Class1()
    {

    }

    public string Id { get; set; }
    public string Value { get; set; }
}

public class Class2
{
    public Class2()
    {

    }

    public string Id { get; set; }
    public string Value { get; set; }
}


........

  public class Class5
 {
    public Class5()
    {

    }

    public string Id { get; set; }
    public string Value { get; set; }
}

Then I have for each of those classes a method that will return a List<Class>.

 public static List<Class1> GetClass1()
    {
        Dictionary<string, string> s = GetSomeResults1();

        List<Class1> _s = new List<Class1>();

        foreach (var item in s)
        {
            Class1 c = new Class1();
            c.Id = item.Key;
            c.Value = item.Value;

            _s.Add(c);
        }

        return _s;
    }


  public static List<Class2> GetClass2()
    {
        Dictionary<string, string> s = GetSomeResults2();

        List<Class2> _s = new List<Class2>();

        foreach (var item in s)
        {
            Class2 c = new Class2();
            c.Id = item.Key;
            c.Value = item.Value;

            _s.Add(c);
        }

        return _s;
    }

  ......

   public static List<Class5> GetClass5()
    {
        Dictionary<string, string> s = GetSomeResults5();

        List<Class5> _s = new List<Class5>();

        foreach (var item in s)
        {
            Class5 c = new Class5();
            c.Id = item.Key;
            c.Value = item.Value;

            _s.Add(c);
        }

        return _s;
    }

Any advise how can I better make this code?

Upvotes: 0

Views: 135

Answers (5)

AndreySarafanov
AndreySarafanov

Reputation: 804

Patrick Hofman's answer is right, but i'd also add that using a BaseClass would allow you to reduce the amount of code working with your classes.

public static List<T> GetClassList() where T:BaseClass
{
    Dictionary<string, string> s = GetSomeResults<T>();

    List<T> _s = new List<T>();

    foreach (var item in s)
    {
        T c = new T();
        c.Id = item.Key;
        c.Value = item.Value;

        _s.Add(c);
    }

    return _s;
}

Changing just this function is not enough though, you also need a way to implement the GetSomeResults() methods. I don't really know what your logic looks like and how different these methods are, but smth like this can help in the worst case when methods are completely different.

public static Dictionary<string, string> GetSomeResults<T>() where T : BaseClass
{
    if (T == typeof(Class1))
    {
        return GetSomeResults1();
    }
    else if (T == typeof(Class2))
    {
    //You got it..
    }
}

Upvotes: 0

Peter
Peter

Reputation: 27944

You can use a base class:

public abstract class BaseClass{
   public string Id { get; set; }
   public string Value { get; set; }
}
public class Class1 : BaseClass
{
    public Class1()
    {
    }
}

public class Class2: BaseClass
{
    public Class2()
    {
    }
}

Now you can make a generic method which returns the interface of List<T> where T is of type BaseClass

public static List<T> GetClass<T>(Func<Dictionary<string, string> func) where T : BaseClass, new()
{
    Dictionary<string, string> s = func();

    List<T> _s = new List<T>();

    foreach (var item in s)
    {
        T c = new T();
        c.Id = item.Key;
        c.Value = item.Value;

        _s.Add(c);
    }

    return _s;
}

Then call:

List<Class2> class2list = GetClass<Class2>(GetSomeResults2);

Upvotes: 0

Jan Kukacka
Jan Kukacka

Reputation: 1265

You can use class inheritance and put common parts of code to a base class like this:

/// <summary>
/// Base class
/// </summary>
public class BaseClass
{
  public BaseClass()
  {
  }

  public string Id { get; set; }
  public string Value { get; set; }

  public virtual List<BaseClass> GetClass();

  protected List<TClass> GetList<TClass> (Dictionary<string, string> s) where TClass : BaseClass, new() {
    List<TClass> _s = new List<TClass>();

    foreach (var item in s)
    {
        TClass c = new TClass();
        c.Id = item.Key;
        c.Value = item.Value;

        _s.Add(c);
    }

    return _s;
  }
}

public class Class1 : BaseClass
  {
    public override List<Class1> GetClass() {
       Dictionary<string, string> s = GetSomeResults1();
       return GetList<Class1>(s);
  }
}

Upvotes: 0

Amit
Amit

Reputation: 15387

I will suggest create a seperate class for

public string Id { get; set; }
public string Value { get; set; }

and call inside class.

Upvotes: 1

Patrick Hofman
Patrick Hofman

Reputation: 156918

Use a base class to put the shared properties and functions in:

public class BaseClass
{
    public string Id { get; set; }
    public string Value { get; set; }

    // shared properties and methods
}

public class Class1 :  BaseClass
{
    // own properties and methods
}

public class Class2 :  BaseClass
{
    // own properties and methods
}

Upvotes: 11

Related Questions