Musings of a PC

Thoughts about Windows, TV and technology in general

Win8 Development: coping without EntityRef and EntitySet

In a recent post, I mentioned that the version of the .Net framework being used by Windows Store apps doesn’t have any support for EntityRef and EntitySet. I have used these extensively in my WP app to link database tables together. I posted on StackOverflow to see if there were any suggestions and one was to use System.Data.SQLite, which provides ADO.NET interfaces to SQLite. However, I was put off this suggestion for a few reasons:

  1. The FAQ says that all System.Data.SQLite objects that implement IDisposable, either directly or indirectly, should be explicitly disposed when they are no longer needed in order to avoid memory leaks. The way it was worded was a bit off-putting in as much as how am I supposed to figure out which objects implement IDisposable, particularly indirectly?
  2. There doesn’t appear to be a WinRT version yet. There are 32-bit and 64-bit Windows versions but what about ARM?
  3. Would this work with SQLite-net or instead of? SQLite-net has good support for asynchronous working which may be lost if this works instead of that library.

So, in the end, I’ve decided to take a simpler approach and deal with it by hand. Where a child table is linked to a parent table, the child has an extra row defined that is a nullable int to store the ID for that parent table. Then, in the class for the parent table, I define an “Add” function like this:

public ChildClass Add(ChildClass child)
    Debug.Assert(this.ID != 0);
    child.ParentClass = this.ID;
    return child;

The purpose of the Assert is to make sure that the parent class has been inserted into the database and therefore has its primary key column set. In my code, when I want to create a new child class, I have a routine called AddChild which then returns the new child object. Because the above Add routine returns the class that is passed to it, this allows me to go:


The AddChild routine creates a new instance of the Child class and then calls _db.Insert(_child) so that the Child instance is added to the database and gets it’s ID value. The rest of AddChild does other bits to the members of the class but doesn’t call _db.Update. I rely on the above line to do the setting of the parent’s ID and calling Update all in one go so that writes to the database are minimised.

The above works but isn’t ideal. One way in which EntityRef and EntitySet are clearly better is that they enforce the correct class usage. For example, if the Child class could be linked to Parent and StepParent, my method wouldn’t do anything to stop me inadvertently setting the child’s ParentClass value to the ID of the StepParent class if I got my logic wrong somewhere. EnttiyRef/EntitySet would handle that because of the type linkage.

One response to “Win8 Development: coping without EntityRef and EntitySet

  1. Pingback: Windows 8 Developer Links – 2012-08-27Dan Rigby | Dan Rigby

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: