mardi 29 décembre 2015

Patterns to manage database relationships in memory

A while ago, I wrote a small framework in C# to manage database relationships (1 to 1, 1 to N) in memory. I've since refactored some of the design and here's an example of the how one might use the current code:

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

To me, this seems like a use of the mediator design pattern with dependency properties. There's a central singleton object, the RelationshipManager, that's used to manage changes to an object's relatives. That static dependency is accessed by each instance through instance properties.

Is there another way to do this, without using dependency properties? I didn't design the original code for this framework, and am curious how else someone might do this.

Aucun commentaire:

Enregistrer un commentaire