관계 자체에 특성이있는 다 - 대 - 다 관계를 모델링하는 데 기본 C# 코드는 어떻게 생겼을까요? 또한이 경우 다 대다는 참조 용이었습니다. 그래서 이것에 대한 가능한 데이터베이스 모델은관계에 속성이있는 many-to-many 관계에 대한 클래스 모델
- 노드
- ID
- 이름
- 설명을
- 관계
- 부모 _ ID
- 이 가장 중요한 (쉽게 것이지) 성분이
Node
클래스의 속성이다Relationships
Relationships_Type
관계 자체에 특성이있는 다 - 대 - 다 관계를 모델링하는 데 기본 C# 코드는 어떻게 생겼을까요? 또한이 경우 다 대다는 참조 용이었습니다. 그래서 이것에 대한 가능한 데이터베이스 모델은관계에 속성이있는 many-to-many 관계에 대한 클래스 모델
Node
클래스의 속성이다 Relationships
Relationships_Typepublic class Node
{
// General properties
public List<Relationship> Relationships { get; set; }
}
public class Relationship
{
public Node Parent { get; set; }
public Node Child { get; set; }
public RelationshipType Type { get; set; }
}
public enum RelationshipType
{
//...
}
Node
이 연결되어 있습니다. public class RelationshipRepository
{
private List<Relationship> relationships = new List<Relationship>();
public IEnumerable<Relationship> GetRelationships(Node node)
{
return relationships.Where(r => r.Child == node || r.Parent == node);
}
public IEnumerable<Relationship> GetChildRelationships(Node node)
{
return relationships.Where(r => r.Parent == node);
}
public IEnumerable<Relationship> GetParentRelationships(Node node)
{
return relationships.Where(r => r.Child == node);
}
public void Add(Node parent, Node child, RelationshipType type)
{
relationships.Add(new Relationship()
{
Parent = parent,
Child = child,
Type = type
});
parent.RelationshipSource = this;
child.RelationshipSource = this;
}
}
public class Node
{
// General properties
public RelationshipRepository RelationshipSource { get; set; }
public IEnumerable<Relationship> Relationships
{
get { return relationships.GetRelationships(this); }
}
public IEnumerable<Relationship> Children
{
get { return relationships.GetChildRelationships(this); }
}
public IEnumerable<Relationship> Parents
{
get { return relationships.GetParentRelationships(this); }
}
}
당신이 하나의 RelationshipRepository
인스턴스를 생성 Add
기능을 사용하여 Node
의 사이에 관계를 추가 할이 허용 것, 그리고 나머지를 다룰 것이다. 영향을받는 Node
중 하나에서 Relationships
, Children
또는 Parents
을 호출하면 어린이, 부모 또는 모든 관계를 확인하기 위해 relationshipSource
이 자동으로 검사됩니다.
public class Node
{
public int Id {get;set;}
public string Name {get;set;}
public string Description{get;set;}
public Dictionary<RelationShipType,IEnumerable<Node>> ChildNodes {get;set;}
}
public enum RelationShipType
{
....
}
감사합니다. Adam - 클래스/객체 세계에서 외래 키 관계가 작동하는 방식을 물어볼 수 있습니까? 즉, 두 개의 노드와 이들을 연결하는 관계가있는 경우 노드 목록에서 노드를 삭제하면 어떻게됩니까? 노드가 목록에서 사라지 겠지만 관계 참조로 인해 관계가 여전히 존재할 것이라고 추측하고 있습니까? 마치 이것을 처리하기 위해 래퍼 메소드 (예 : AddNode)를 작성하는 것은 프로그래머에게 달린 것입니까? 즉 데이터베이스 외래 키 제약 조건과 동일한 기본 제공 기능이 없습니까? – Greg
@Greg : 할 수는 있지만'Child'와'Parent' 속성 인'internal'에 대한 세터를 만들고, 'RelationshipRepository'에 원하는 것을 이루기위한 함수를 추가하고, 비즈니스 로직을 유지해야합니다. 다른 어셈블리에서. 이로 인해 개발자는 저장소를 통해 변경 작업을 수행해야합니다. 'RemoveChild'와'RemoveParent'와 같은 것을 저장소에있는 함수를 호출하는'Node' 클래스에 추가 할 수도 있습니다. 'AddChild'와'AddParent'도 마찬가지입니다. –