I have written a class to represent ‘Treeview-like’ data, which can be simplified as:
public class Item { public string Name { get; set; } public KPI AssociatedKPI { get; set; } public List<Item> Children { get; set; } }
To each ‘branch’ of the Tree, can be associated a KPI, which can be simplified as:
public class KPI { public string Name { get; set; } public double Value { get; set; } }
My database looks like
+------+-------+ + Name + Value + +------+-------+ + foo + 5 + + bar + 4.56 + +------+-------+
The tree structure, with the names of the KPIs is supposed to be filled in already.
I am struggling to decide where I should put the method to fill the values of the KPIs.
- First solution: In the KPI class
SELECT value FROM KPI WHERE NAME = 'foo'
It seems like it respects the SRP, the KPI class is responsible for the values of the KPI.
However, each KPI is unaware of the existence of others, and I need to have as many queries as I have branches in my tree.
- Second solution: In the Item class
I would get the names of all the KPIs in the descendants of this Item
, and get a query looking like
SELECT Value FROM KPI WHERE Name IN ('foo', 'bar')
This way, I only need one per root of the tree (so usually, a single one).
My concern is that it breaks SRP, the Item
should not be in charge of setting the values, should it?
In conclusion:
What would be the best way of doing it? Is there a third, better way I did not think about?
In this case, performance is not really an issue, the table and the tree are small, but I am learning, and would like to learn about the best practices.
Thanks,