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.
Building
shouldn't be static. (2) You shouldn't haveGetX()
orSetX()
methods in C#: use getters and setters instead. (3) YourList<T>
-type fields can bereadonly
. (4) You shouldn't returnList<T>
inGetColumns
: use a more generic type, such asIEnumerable<T>
. (5) Don't useList<T>
in parameters: use a more generic type, such asICollection<T>
. (6)void Group()
may not be that intuitive, especially given the popularity ofIEnumerable<T>.GroupBy()
which returns a value instead of modifying state. – Arseni Mourzenko Dec 19 '14 at 15:34