gpt4 book ai didi

c# - 转换为使用泛型

转载 作者:行者123 更新时间:2023-11-30 17:39:05 25 4
gpt4 key购买 nike

我已经尝试了好几次,但总是让自己陷入循环。您将如何将此代码更改为基于泛型的方法?此代码遵循四人组组合模式。

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)
{
}
}

实例化此对象映射的示例:

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;
}

虽然我的示例非常简单,但我们的实体结构并不那么简单。我们目前有 7 个不同的实体,几乎任何类型的实体都可以是任何类型实体的子实体,并且它的 sibling 也可以是各种类型。

TIA

编辑:试图澄清: child (和 child 的 child )可以是任何实体类型(机构、用户、官员、客户等)。虽然所有实体都具有共同的属性基础,但每个对象的其余部分彼此不同。从数据库中提取数据时,我可能会请求一个机构,并希望在该机构下有整个层次结构。直系后代可以包括所有类型,每个 child 都可以有包括所有类型的后代。非常凌乱,非常灵活。

最佳答案

这可以使您的类层次结构具有强类型并符合 CreateSample 代码:

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)
{
}
}

然后CreateSample需要这样修改:

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 };
}

关于c# - 转换为使用泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35905708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com