Search code examples
c#code-duplication

How to avoid duplication?


I'm making a C# application. The application has two classes and multiple methods. While writing the code I stumbled on a problem. I use the same two variables (XList and YList) and one method in both classes. And probably I will need more classes with this code. So I created a duplication problem. Below is a simple version of my code:

public class A {
  private testEntities db = new testEntities();
  public List<int> XList = new List<int>();
  public List<int> YList = new List<int>();

  public void GetAllInfo()
  {
    // Get the data from a database and add to a list
    XList = db.Table1.ToList();
    YList = db.Table2.ToList();
  }

  public void DoStuff()
  {
    // Do Stuff with XList and YList
  }
}

public class B {
  private testEntities db = new testEntities();
  public List<int> XList = new List<int>();
  public List<int> YList = new List<int>();

  public void GetAllInfo()
  {
    // Get the data from a database and add to a list (the same as in class A)
    XList = db.Table1.ToList();
    YList = db.Table2.ToList();
  }

  public void DoDifferentStuff()
  {
    // Do ddifferent stuff with XList and YList then in class A
  }
}

My question is what is the best way to solve this duplication problem?

After some research I found that I can probably solve this with inheritance or composition. I also read that people choose composition over inheritance. So I wrote the following code to solve the duplication:

public class DataPreparation
{
  private testEntities db = new testEntities();
  public List<int> XList = new List<int>();
  public List<int> YList = new List<int>();

  public void GetAllInfo()
  {
    // Get the data from a database and add to a list
    XList = db.Table1.ToList();
    YList = db.Table2.ToList();
  }

  // Implement other methods
}

public class A 
{
  public void MethodName()
  { 
    DataPreparation dataPreparation = new DataPreparation();
    dataPreparation.GetAllInfo();

    UseDataX(dataPreparation.XList);
    UseDataY(dataPreparation.YList);

    // Implementation UseDataX() and UseDataY()
  }
}

public class B 
{
  public void MethodName()
  { 
    DataPreparation dataPreparation = new DataPreparation();
    dataPreparation.GetAllInfo();

    VisualizeDataX(dataPreparation.XList);
    VisualizeDataY(dataPreparation.YList);

    // Implementation VisualizeDataX() and VisualizeDataY()
  }
}

As you can see I made a class that handles getting the data from the database. And that the class A and B use the DataPreparation class. But is this the best way to solve the duplication? Or should I use inheritance or something different?


Solution

  • I think you should probably only have one method called DoStuff() rather than one called DoStuff() and another called DoDifferentStuff().

    Then you can create an ABC to implement the common code, and have an abstract DoStuff() method that is implemented differently in the derived classes:

    public abstract class Base
    {
        private testEntities db = new testEntities();
    
        public List<int> XList = new List<int>();
        public List<int> YList = new List<int>();
    
        public void GetAllInfo()
        {
            // Get the data from a database and add to a list (the same as in class A)
            XList = db.Table1.ToList();
            YList = db.Table2.ToList();
        }
    
        public abstract void DoStuff();
    }
    
    public class A: Base
    {
        public override void DoStuff()
        {
            // Do Stuff with XList and YList
        }
    }
    
    public class B: Base
    {
        public override void DoStuff()
        {
            // Do ddifferent stuff with XList and YList then in class A
        }
    }
    

    (I also think it's a bad idea to have public fields like that - but I'm guessing/hoping that this is just sample code and your real code doesn't have those...)

    Other code (except code that creates an A or a B) would use the object via the Base class type.