Object hierarchy

Feb 18, 2010 at 12:18 PM


I am new to Opf3. It seems to be very easy to use. However, I cannot figure out how (and if it is even possible) to create a hierarchy of objects. I have successfully tested M-N relationship scenarios between 2 classes/tables.

But what I need is something more. I need to create a hierarchy of several levels.

An example (this is a thought out simplification; I think it corresponds to my real issue.):

I have 3 tables: House, Season, Person and a relation that assigns Person to a House *through* a season (depending on a season, there are different people in a house). And I would like to have a possibility to expand it further - like something that a Person can have several cars.

As the result, I would like to have the following hierarchy:

Set/List of houses -> each house has a set/list of seasons -> each season has a set/list of persons -> etc...

Is this possible with Opf3?

Thank you in advance,



Feb 19, 2010 at 4:07 PM
Edited Feb 19, 2010 at 4:26 PM

Absolutely possible! You need to make use of ObjectHolders and the Relation attribute to accomplish what you're wanting. Here's a small example.

First you make a class to hold the database fields. Using your example here's how I'd do it.


[Persistent("Houses", PoolSize = 10)]
public partial class House : ISelfContainingObject

     private int _Id = 0;
     private string Name = "";
     private DateTime Timestamp = DateTime.Now;

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

     [Field("Name", AllowDBNull=false)]
     public string Name
          get { return _Name; }
          set { _Name = value; }

     [Field("Timestamp", AllowDBNull=false)]
     public DateTime Timestamp
          get { return _Timestamp; }
          set { _Timestamp = value; }
     public ObjectInfo _ObjectInfo = new ObjectInfo();
     ObjectInfo ISelfContainingObject.ObjectInfo
          get { return _ObjectInfo ; }
          set { _ObjectInfo = value; }



This is simple class that will hold information for a single row of data in the Houses table. Now as you mentioned, you have a relation between a House and a its' seasons. You'll need to add a new property to hold that information like so.



[Relation("Id=HouseId", PersistRelationship = PersistRelationships.ParentFirst)]
private ObjectSetHolder<Seasons> _SeasonsRelation = new ObjectSetHolder<Seasons>();
public ObjectSet<Seasons> SeasonRelation
    get { return _SeasonsRelation.InnerObject; }
    set { _SeasonsRelation.InnerObject = value; }


Adding this property to your class will now hold the One-To-Many relationship in this object. Now you have a Enumerable list that you can work with. If you have the need for just a single relationship (One-To-One) then the code is slightly different.

[Relation("Id=HouseId", PersistRelationship = PersistRelationships.ParentFirst)]
private ObjectHolder<Seasons> _SeasonsRelation = new ObjectHolder<Seasons>();
public Seasons SeasonRelation
get { return _SeasonsRelation.InnerObject; }
set { _SeasonsRelation.InnerObject = value; }


Feb 22, 2010 at 9:30 AM
Edited Feb 22, 2010 at 9:31 AM

Thank you for the reply. But I think that it is not what I am looking for.

Just to be sure: your solution assumes that there is a HouseId field/property in the Seasons. So it is only 1-M relation. Correct?

I would have something like this:

1) House, Season and Person

2) A assignment table relating these 3 objects. Something like this:


HouseId | SeasonId | PersonId
1       | 1         | 1
1       | 1         | 2
1       | 2         | 1
2       | 1         | 3
2       | 2         | 2


So basically, there is a M-N-O relation. What I need is to create a tree such that House has a list of Seasons that have a list with *corresponding* persons. I.e. persons, that are assigned to a season only for a house that is connected to the season.

As I wrote in the first post, it is simple to use M-N relation. But I cannot use it in such a chain - If I used M-N mapping between Seasons and Persons, I would obtain "Person 3" even for the "House 1".

Is this "chaining" possible to do or am I stiil overlooking something very obvious?



Feb 24, 2010 at 6:39 PM

Why not use your assignment table to create the others for you?  You would lose a direct relationship between House, Season and Person but theses are temporary relationships anyway and not permanent so use what is permanent. 

Another option - may or may not fit your needs is to use sprocs

exec usp_GetHouseBySeason(@seasonid)

and in your it returns house with the personid and the only relationship you need is House to Person because Season is known.   You of course can create a few different sprocs to handle the different scenarios and access them all in different methods of an ObjectSearcher.






Feb 25, 2010 at 4:17 PM

In my real application, I can very well select what I need and build my structures in the code. So it's very similar to the sprocs approach only that the logic is not dependent on the storage. It also minimizes the communication with the database.

It was just interesting for me to know whether it was possible with Opf3 or not. Apparently not. But don't get me wrong, I like the framework and it helps me to save some of that ugly SQL code in ADO.NET :)