3

I'm writing a CAD program. Let's say I have in input class, this class reads various data from a text file and creates lots of lists/dictionaries and .... These data need to be accessed by other methods in other classes to be modified. Now here is how I have done it so far:

I have one static class: Building.cs When I create/or load a project this class holds all the data like list of columns, beams, points, etc. All of these are stored as private fields. I can access these using the class's public methods like GetColumns or GetPoints ...

Now I also have non-static classes. They contain 2-3 public methods. and do some stuff on various parts of the building.

public static class Building
{
    private static List<Column> columns;
    private static List<Beams> beams;
    private static List<Points> points;

    public static List<Column> GetColumns() 
    {
        return Columns;
    }
}

public class ColumnsService()
{
    private List<Columns> columns;
    public GroupColumns(List<Columns> columns)
    {
        this.columns = columns;
    }

    public void Group()
    {
        // group columns
    }
}

var columns = Building.GetColumns();
var columnsService = new ColumnsService(columns);
columnsService.Group();

I was wondering is this the way to go? How else can I store the data. The data needs to be accessible throughout the lifetime of the program to most of the classes.

  • 2
    A few notes: (1) Building shouldn't be static. (2) You shouldn't have GetX() or SetX() methods in C#: use getters and setters instead. (3) Your List<T>-type fields can be readonly. (4) You shouldn't return List<T> in GetColumns: use a more generic type, such as IEnumerable<T>. (5) Don't use List<T> in parameters: use a more generic type, such as ICollection<T>. (6) void Group() may not be that intuitive, especially given the popularity of IEnumerable<T>.GroupBy() which returns a value instead of modifying state. – Arseni Mourzenko Dec 19 '14 at 15:34
  • How can I persist the data? Should I use a singleton pattern? – Vahid Dec 19 '14 at 15:45
  • 1
    You can use dependency injection to pass the instance of the class to the methods which need it. – Arseni Mourzenko Dec 19 '14 at 15:50
4

Because in your first question you professed a lack of knowledge about dependency injection, I will explain it in simple terms:

public class ThingThatNeedsABuilding
{
  private Building building;
  public ThingThatNeedsABuilding(Building building)
  {
    this.building = building;
  }
}

public class OtherThingThatNeedsABuilding
{
  private Building building;
  public OtherThingThatNeedsABuilding(Building building)
  {
    this.building = building;
  }
}

public class Application
{
  public static void Main(string[] args)
  {
    var building = new Building();
    var thing1 = new ThingThatNeedsABuilding(building);
    var thing2 = new OtherThingThatNeedsABuilding(building);
  }
}

It's really that simple. The same instance of building is now shared between those two instances and nothing else. This is important. You do not want more stuff than needed to be available. This helps you avoid it.

If an object needs something, by all means give that something to it. Making it globally accessible to all objects makes it hard to tell if your program will even run, because an issue in the configuration of the dependency may cause a crash, but you can't tell at initialization that an object even has that dependency.

There are other important reasons to inject dependencies, but that one is very simple to understand and very hard to reject.

  • Thanks for the simple answer. I was wondering why not using Extension Methods? For example instead of defining various classes that operate on building class, why not use an extension method and pass building to it? public static void DoSomething(this Building building){} Is there a drawback to this? – Vahid Dec 19 '14 at 20:31
  • @Vahid: Extension methods fulfill a very important but very specific role. They are typically used to operate on interfaces or third-party classes that you cannot extend. If you have access to the Building class, you can simply add public methods to it to do the things you seem to want to use extension methods for. – Magus Dec 19 '14 at 20:37
  • I need like 300 of methods? It doesn't seem reasonable to add all these to the Building class. Does it? I have asked this question here stackoverflow.com/questions/27573660/… – Vahid Dec 19 '14 at 20:46
  • 1
    There is no way you need 300 methods to act on the properties of the Building class. 300 extension methods would be just as bad as 300 public methods within the class. You need to split some classes up somehow, if you are having this problem. – Magus Dec 19 '14 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.