Skip to main content
added 2009 characters in body
Source Link
Enigmativity
  • 117.5k
  • 12
  • 101
  • 184

Just to get the ball rolling here's howThis works to make your class hierarchy strongly-typed & comply with the CreateSample code:

public abstract class AdjacencyTreeBase<T> where T : AdjacencyTreeBase<T> { 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(T c); public abstract void RemoveChild(T c); public abstract List<T> ListChildren(); public abstract void AddChildren(List<T> c); public abstract void ReplaceChildren(List<T> c); } public class AdjacencyTree<T> : AdjacencyTreeBase<T>abstract whereclass TAdjacencyTree : AdjacencyTree<T>AdjacencyTreeBase<AdjacencyTree> { private List<T>List<AdjacencyTree> _children = new List<T>List<AdjacencyTree>(); public List<T>List<AdjacencyTree> Children { get { return _children; } set { _children = value; } } public AdjacencyTree(int entityId, int entityTypeId) : base(entityId, entityTypeId) { } public override void AddChild(TAdjacencyTree component) { _children.Add(component); } public override void AddChildren(List<T>List<AdjacencyTree> c) { _children = c; } public override void ReplaceChildren(List<T>List<AdjacencyTree> c) { _children = c; } public override void RemoveChild(TAdjacencyTree component) { _children.Remove(component); } public override List<T>List<AdjacencyTree> ListChildren() { return _children; } } public class AdjacencyAgency : AdjacencyTree<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<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<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) { } } 

But this doesn't seem to support yourThen CreateSample method. Can you please respondneeds to my comment under your question and I'll work onbe modified like this answer some more.:

public List<AdjacencyTree> 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 new List<AdjacencyTree>() { agencyThatAlreadyExists }; } 

Just to get the ball rolling here's how to make your class hierarchy strongly-typed:

public abstract class AdjacencyTreeBase<T> where T : AdjacencyTreeBase<T> { 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(T c); public abstract void RemoveChild(T c); public abstract List<T> ListChildren(); public abstract void AddChildren(List<T> c); public abstract void ReplaceChildren(List<T> c); } public class AdjacencyTree<T> : AdjacencyTreeBase<T> where T : AdjacencyTree<T> { private List<T> _children = new List<T>(); public List<T> Children { get { return _children; } set { _children = value; } } public AdjacencyTree(int entityId, int entityTypeId) : base(entityId, entityTypeId) { } public override void AddChild(T component) { _children.Add(component); } public override void AddChildren(List<T> c) { _children = c; } public override void ReplaceChildren(List<T> c) { _children = c; } public override void RemoveChild(T component) { _children.Remove(component); } public override List<T> ListChildren() { return _children; } } public class AdjacencyAgency : AdjacencyTree<AdjacencyAgency> { 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<AdjacencyUser> { 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<AdjacencyClient> { 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) { } } 

But this doesn't seem to support your CreateSample method. Can you please respond to my comment under your question and I'll work on this answer some more.

This works to make your class hierarchy strongly-typed & comply with the CreateSample code:

public abstract class AdjacencyTreeBase<T> where T : AdjacencyTreeBase<T> { 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(T c); public abstract void RemoveChild(T c); public abstract List<T> ListChildren(); public abstract void AddChildren(List<T> c); public abstract void ReplaceChildren(List<T> c); } public abstract class AdjacencyTree : AdjacencyTreeBase<AdjacencyTree> { private List<AdjacencyTree> _children = new List<AdjacencyTree>(); public List<AdjacencyTree> Children { get { return _children; } set { _children = value; } } public AdjacencyTree(int entityId, int entityTypeId) : base(entityId, entityTypeId) { } public override void AddChild(AdjacencyTree component) { _children.Add(component); } public override void AddChildren(List<AdjacencyTree> c) { _children = c; } public override void ReplaceChildren(List<AdjacencyTree> c) { _children = c; } public override void RemoveChild(AdjacencyTree component) { _children.Remove(component); } public override List<AdjacencyTree> 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) { } } 

Then CreateSample needs to be modified like this:

public List<AdjacencyTree> 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 new List<AdjacencyTree>() { agencyThatAlreadyExists }; } 
Source Link
Enigmativity
  • 117.5k
  • 12
  • 101
  • 184

Just to get the ball rolling here's how to make your class hierarchy strongly-typed:

public abstract class AdjacencyTreeBase<T> where T : AdjacencyTreeBase<T> { 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(T c); public abstract void RemoveChild(T c); public abstract List<T> ListChildren(); public abstract void AddChildren(List<T> c); public abstract void ReplaceChildren(List<T> c); } public class AdjacencyTree<T> : AdjacencyTreeBase<T> where T : AdjacencyTree<T> { private List<T> _children = new List<T>(); public List<T> Children { get { return _children; } set { _children = value; } } public AdjacencyTree(int entityId, int entityTypeId) : base(entityId, entityTypeId) { } public override void AddChild(T component) { _children.Add(component); } public override void AddChildren(List<T> c) { _children = c; } public override void ReplaceChildren(List<T> c) { _children = c; } public override void RemoveChild(T component) { _children.Remove(component); } public override List<T> ListChildren() { return _children; } } public class AdjacencyAgency : AdjacencyTree<AdjacencyAgency> { 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<AdjacencyUser> { 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<AdjacencyClient> { 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) { } } 

But this doesn't seem to support your CreateSample method. Can you please respond to my comment under your question and I'll work on this answer some more.