I've tried several times and I keep getting myself into a loop. How would you change this code to a generics based approach? This code follows the Gang of Four Composite Pattern.
public abstract class AdjacencyTreeBase { public AdjacencyTreeBase(int entityId, int entityTypeId) { EntityId = entityId; EntityTypeId = entityTypeId; } public long? Id { get; set; } public int? SystemId { get; set; } public int EntityId { get; set; } public int EntityTypeId { get; set; } public bool? isActive { get; set; } public long? lft { get; set; } public long? rgt { get; set; } public abstract void AddChild(AdjacencyTreeBase c); public abstract void RemoveChild(AdjacencyTreeBase c); public abstract List<AdjacencyTreeBase> ListChildren(); public abstract void AddChildren(List<AdjacencyTreeBase> c); public abstract void ReplaceChildren(List<AdjacencyTreeBase> c); } public class AdjacencyTree : AdjacencyTreeBase { private List<AdjacencyTreeBase> _children = new List<AdjacencyTreeBase>(); public List<AdjacencyTreeBase> Children { get { return _children; } set { _children = value; } } public AdjacencyTree(int entityId, int entityTypeId) : base(entityId, entityTypeId) { } public override void AddChild(AdjacencyTreeBase component) { _children.Add(component); } public override void AddChildren(List<AdjacencyTreeBase> c) { _children = c; } public override void ReplaceChildren(List<AdjacencyTreeBase> c) { _children = c; } public override void RemoveChild(AdjacencyTreeBase component) { _children.Remove(component); } public override List<AdjacencyTreeBase> ListChildren() { return _children; } } public class AdjacencyAgency : AdjacencyTree { public string agency_name { get; set; } public string customer_number { get; set; } public string agency_type { get; set; } public AdjacencyAgency(int entityId, int entityTypeId) : base(entityId, entityTypeId) { } } public class AdjacencyUser : AdjacencyTree { public string officer_number { get; set; } public string last_name { get; set; } public string first_name { get; set; } public string middle_initial { get; set; } public AdjacencyUser(int entityId, int entityTypeId) : base(entityId, entityTypeId) { } } public class AdjacencyClient : AdjacencyTree { public string last_name { get; set; } public string first_name { get; set; } public string middle_initial { get; set; } public string ssn { get; set; } public AdjacencyClient(int entityId, int entityTypeId) : base(entityId, entityTypeId) { } } A sample of instantiating this object map:
public List<AdjacencyTreeBase> CreateSample() { // build bottom of tree objects... var client1 = new AdjacencyClient(1, 4) { first_name = "Pic'nic", last_name = "Basket #1", ssn = "123-45-6789" }; var client2 = new AdjacencyClient(2, 4) { first_name = "Pic'nic", last_name = "Basket #2", ssn = "234-56-7890" }; var client3 = new AdjacencyClient(3, 4) { first_name = "Bear", last_name = "Cave", ssn = "345-67-8901" }; var client4 = new AdjacencyClient(4, 4) { first_name = "Picnic", last_name = "Table", ssn = "456-78-9012" }; // build the next level up and add the children... var officer1 = new AdjacencyUser(1, 3) { first_name = "Yogi", last_name = "Bear", officer_number = "YB123" }; officer1.AddChild(client1); officer1.AddChild(client2); var officer2 = new AdjacencyUser(2, 3) { first_name = "Park", last_name = "Ranger", officer_number = "PR123" }; officer2.AddChild(client3); officer2.AddChild(client4); // build the top of the tree and add the middle children... var agencyThatAlreadyExists = new AdjacencyAgency(1, 2) { agency_name = "Jellystone", agency_type = "Park", }; agencyThatAlreadyExists.AddChild(officer1); agencyThatAlreadyExists.AddChild(officer2); return agencyThatAlreadyExists; } While my sample is pretty simple, our entity structure is not quite so simple. We currently have 7 different entities and pretty much any type of entity can be a child of any type of entity and its siblings could be various types as well.
TIA
EDIT: To try to clarify: The children (and children of children) can be of any entity type (agency, user, officer, client, etc). While all entities have a base of properties in common, the rest of each object is different from one another. When pulling from the database, I may make a request for an agency and want the entire hierarchy underneath that one agency. Direct descendants could include all types, the each child could have descendants which include all types. Very messy, very flexible.
CreateSamplemethod seems to suggest that you want to make a composite type. Can you be clearer on the question you're asking?