Thursday, August 04, 2005

[Opf3] Object(Set|List)Holder and inheritance

To implement relations in Opf3 persistent objects you need the ObjectSetHolder, the ObjectHolder or the ObjectListHolder. An example for a relation is the following:


[Persistent("FOO")]
public sealed class Foo
{
    private int _id;
    private int _name;

    [Relation("ID = FooID")]
    private ObjectSetHolder<Bar> _bars = new ObjectSetHolder<Bar>();

    public ObjectSet<Bar> Bars
    {
        get { return _bars.InnerObject; }
    }

    [Field("ID", Identifier = true, AllowDBNull = false, AutoNumber = true)]
    public int ID
    {
        get { return _id; }
        set { _id = value; }
    }

    [Field("NAME", AllowDBNull = false)]
    private string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

[Persistent("BAR")]
public sealed class Bar
{
    private int _id;
    private int _fooID;

    [Field("ID", Identifier = true, AllowDBNull = false, AutoNumber = true)]
    public int ID
    {
        get { return _id; }
        set { _id = value; }
    }

    [Field("FOO_ID", AllowDBNull = false)]
    private int FooID
    {
        get { return _fooID; }
        set { _fooID = value; }
    }
}


This pattern breaks with inheritance and the current implementation of the mapping engine. If you inherit from Foo, for example you create Foo1, the private ObjectSetHolder and the RelationAttribute on that instance isn't detected anymore. Without the RelationAttribute it is impossible for Opf3 to retrieve the related persistent objects. The only solution for now is to set the ObjectSetHolder to protected. As mentioned in this blog entry it is only required when you inherit from Foo. If you don't inherit you do not need to set the ObjectSetHolder protected. In that case "private" is fine.



    [Relation("ID = FooID")]
    protected ObjectSetHolder<Bar> _bars = new ObjectSetHolder<Bar>();

0 Comments:

Post a Comment

<< Home