|
Article relates to
|
Telerik OpenAccess ORM
|
|
Created by
|
Zoran Kostov
|
|
Last modified
|
June 01, 2009
|
|
Last modified by
|
Zoran Kostov
|
One of the most challenging tasks for a web developer that is using OpenAccess is how-to handle the problems that might be caused by concurrent updates and deletes on the stored data. There are two approaches of handling the process in the appropriate manner - the first is involves using
OpenAccessDataSource component for managing CRUD operations. All the logic for detecting and exposing a concurrent update or delete is built in the OpenAccessDataSurce and it is very straight-forward to track concurrent issues there.
When detecting such problem during an update or delete operation
OpenAccessDataSource throws an
OptimisticVerificationException which can be handled in the event handlers for the
updating and
deleting events.
In more advanced web projects though using a data source is not always an option. Queries are executed in code-behind and all CRUD operations are handled in that manner.
The answer for all concurrency concerns in those situations comes with the implementation of the
IDataObjectKey interface to the persistent objects that are to be modified or deleted.
The IDataObjectKey property is a unique string for every persistent object that contains information about the identity and version of the object. Because the version of a persistent object is updated on modification this key can be used to give us information on weather a certain object has been modified.
Sample implementation:
There are entities of the Person type to be presented in a
RadGrid. The grid is bound with a simple Linq query returning all the Person persistent objects from the database.
Our Person class implements the IDataObjectKey interface:
The RadGrid has the edit command enabled. Our solution for the concurrent updates is the following:
• When the
Edit button is pressed we obtain the person object to be updated and store its
DataObjectKey property into the
Session state. We do that operation in the
EditCommand event handler of the RadGrid:
• After typing the new values into the edit form of the grid the
Update button is pressed. We obtain the record once again in the code-behind but this time using the static
DataObjectKey.Check(string dataObjectKey, IObjectCotnext context) method. In this method OpenAccess checks if the instance represented by the DataObjectKey has been modified since the DataObjectKey was obtained. If the instance contains a new DataObjectKey e.g. it has a new version, an
OptimisticVerificationException is thrown, else the persistent object is returned and we can commit an update on it.
From this point on it is fairly easy for one to implement his/her own logic for proceeding with the situation. For example the Check() method can be called in a try/catch block so if there is a concurrency problem we can just prompt the user with appropriate message.
The code from the sample can be downloaded from Telerik OpenAccess
Code Library.