1
A while ago,我用C#編寫了一個小框架來管理內存中的數據庫關係(從1到1,從1到N)。因爲我已經重構的一些設計和這裏的人們可能會如何使用當前代碼的一個例子:管理內存中數據庫關係的模式
class A : FrameworkEntity
{
public static readonly Relationship1To1<A, B> relationship
= RelationshipManager.Instance.Relate1To1<A, B>();
public B B
{
get { return relationship.GetForward(this); }
set { relationship.SetForward(this, value); }
}
public C Parent
{
get { return C.relationship.GetReverse(this); }
}
}
class B : FrameworkEntity
{
public A A
{
get { return A.relationship.GetReverse(this); }
set { A.relationship.SetReverse(value, this); }
}
}
class C : FrameworkEntity
{
public static readonly Relationship1ToN<C, A> relationship
= RelationshipManager.Instance.Relate1ToN<C, A>();
// the implementation will call the RelationshipManager
// to update the relationship on add/update/delete
public ICollection<A> Children
{
get { return relationship.GetFoward(this); }
}
}
對我來說,這似乎像一個dependency properties使用mediator design pattern的。有一箇中心單體對象,RelationshipManager
,用於管理對象親屬的更改。每個實例通過實例屬性訪問該靜態依賴關係。
是否有另一種方法來做到這一點,而不使用依賴屬性?我沒有設計這個框架的原始代碼,並且很好奇,有人可能會這樣做。
如果您正在尋找設計模式 - Martin Fowler的有與關係數據結構相關的一組模式特別是管理相關表等,請參閱「對象 - 關係結構模式」http://martinfowler.com/eaaCatalog/ – sll