Querying Header and Picture Data

First we get the Header data:

PersistenceManager pm = new PersistenceManager();

Query q = pm.NewQuery(typeof(PictureHeader), null);

IList l = q.Execute();

foreach (PictureHeader ph in l)

    Console.WriteLine(ph.Name);

You can bind IList directly to a DataGrid for user interface integration. After selecting a PictureHeader object that has the Name ph you get the picture with the following code:

PersistenceManager pm = new PersistenceManager();

Query q = pm.NewQuery(typeof(PictureHeader), null);

IList l = q.Execute();

// Normally we'd show the headers in an UI

foreach (PictureHeader ph in l)

    Console.WriteLine(ph.Name);

 

// Select a PictureHeader and find the corresponding Picture

Picture p = (Picture) pm.FindObject(typeof(Picture), ((PictureHeader)l[0]).NDOObjectId.Id.Value);

Image image = p.Image;

Console.WriteLine("Picture: " + p.Name + ": " + image.Height.ToString() + "x" + image.Width.ToString());

Note that this scenario only works because the two classes PictureHeader and Picture are mapped to the same table.

If you want to map the two classes to different tables, you need polymorphy. The same task could also be solved such that the PictureHeader class holds a relation with cardinality 1 to the Picture class. This approach also ensures that the picture is only loaded if it is really needed.

This solution is part of the tutorial samples. You can find the tutorial source code in the Tutorial folder beneath your NDO installation folder. The code up to here is placed in the Directory  NDOTravelExpenses-Step 5.

The next and last tutorial section is about polymorphy.