用于管理内存中数据库关系的模式

时间:2015-12-30 03:07:15

标签: c# design-patterns

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

对我而言,这似乎是mediator design patterndependency properties的使用。有一个中心单例对象RelationshipManager,用于管理对象亲属的更改。每个实例都通过实例属性访问该静态依赖项。

有没有其他方法可以做到这一点,而不使用依赖属性?我没有为这个框架设计原始代码,我很好奇别人怎么可能这样做。

1 个答案:

答案 0 :(得分:0)

Sqlite显然 - 是一个C#包装器,但它是does look to be maintained。在内存中创建一个Sqlite数据库似乎是理想的(取决于你要做的'查询','加入'等工作的复杂性)并且它占用空间很小。

(这可能高于/超出您想要的范围,但实际获得真正的查询/加入支持等可能是值得的)