This project is read-only.

Best way to Update on a object graph

Nov 29, 2010 at 2:00 PM

What is the best way to perform updates using NHydrate?

Let's say I have four tables (tableA, tableB, tableC and tableD), and the appropriate business objects (objectA, objectB, objectC and objectD) generated by NHydrate:

  • The object map is such that objectA references a collection of objectB objects, and a collection of objectC objects. In turn, each of these objectC objects in the collection in turn references a collection of objectD objects

The user always interacts with objectA objects for CRUD functionality (but can traverse the object graph to access any data they need). So to do an update, the user will retrieve the relevant objectA, move around the graph making changes as is their wont and send the object back to the db for persistence. Having had a look at the API for the generated objectACollection class, I can see there is an UpdateData method that allows me to make individual changes, but is there a way of doing all the updates in one go (since there could be many)? 

Cheers, Matt.

Nov 30, 2010 at 12:01 AM

When you load an object and walk its relationships, the necessary objects are loaded as you walk. So in your case you have objectA. You then walk its objectBList property and get a list of all objectB types for objectA. These are all loaded in an in-memory database called a subdomain. No matter how many changes you make or how many objects you load you can simply call one of the collections "Persist" method and all changes are saved back to store. There is nothing more to do. In summary just call the Persist property of any collection int the subdomain.

var object1Collection = object1Collection.RunSelect();
object1Collection.First().Name = "qqq"; 
object1Collection.Persist(); 

The "UpdateData" static method is used to make bulk updates. This is not relevant to your scenario.