Tuesday, June 21, 2005

[Opf3] How does Opf3 decide if an persistent has been changed

How does Opf3 that actually do?

In the case you are using a ConcurrencyManager that's the work of that class. The Md5ConcurrencyManager (for example) creates the Md5 of all the properties of the current object and checks that Md5 with the one that has been created after the last operation on the storage.

Example:

// .. Other code.
// Create an ObjectContext.
ObjectContext context = new ObjectContext(storage);
context.ConcurrencyManager = new Md5ConcurrencyManager();

// Loads an user object from the storage. While loading the
// Md5 for that object is generated and stored internally with the
// data for the object.
User user = context.GetObject<User>("Name = {0}", "john");

// While saving the object the Md5ConcurrencyManager creates a new
// Md5 checksum on the properties of the object. Since we haven't
// changed anything the object is not saved.
context.PersistChanges(user);

// We change something.
user.Name = "New Name";
// The Md5 of the current instance is created and the
// Md5ConcurrencyManager understands that the object has been changed
// in memory since the current Md5 is different from the on that
// has been created during load.
// After the update the Md5 for the instance is set to the new one.
context.PersistChanges(user);


If you are working without any ConcurrencyManager, Opf3 fires an update query for every object that is persisted. Not using a ConcurrencyManager is only useful if you do a batch update. There you don't need those checks at all.

This example shows how this issue is solved with the Md5ConcurrencyManager (the only one that comes with Opf3 until now). If you are implementing any other ConcurrencyManager (by implementing the IConcurrencyManager interface) you have to deal with that issue by your own. You could, for example, have each persistent implement an interface that allows the ConcurrencyManager to check if the object has been changed.

0 Comments:

Post a Comment

<< Home