Telerik blogs

Telerik OpenAccess ORM ObjectScope comes packed with all the events that are needed for managing the lifecycle of your persistent objects. You have events for Add, Remove, Refresh and Update actions. They come in –ed and –ing flavors which would mean that you can successfully iterate with your data before the changes have been made (-ing events) and after the changes have been done(-ed events) . You can subscribe to any or all of them and successfully perform some business logic over your persistent objects.

Here is a basic implementation of a generic (in terms of availability for both ObjectScope and ObjectContainer) class that will subscribe the caller to all –ing events.

public class Tracker
   
{
       
private IObjectContext context;       
       
private string log;

       
public Tracker(IObjectContext cnt, string logFile)
       
{
           
context = cnt;
           
log = logFile;
       
}
       
public void Start()
       
{
           
context.Tracking.Adding += new AddEventHandler(Add);
           
context.Tracking.Changing += new ChangeEventHandler(Change);
           
context.Tracking.Removing += new RemoveEventHandler(Remove);
           
context.Tracking.Refreshing += new RefreshEventHandler(Refresh);
       
}
       
public void Stop()
       
{
           
context.Tracking.Adding -= new AddEventHandler(Add);
           
context.Tracking.Changing -= new ChangeEventHandler(Change);
           
context.Tracking.Removing -= new RemoveEventHandler(Remove);
           
context.Tracking.Refreshing -= new RefreshEventHandler(Refresh);
       
}
}

Note that the context in this class is of type IObjectContext which can hold both IobjectScope and IObjectContainer instances. The log field will hold the custom text that we will apply in each event. So here is some basic implementation of some methods. Note that those methods are really basic and their aim is to just show you the functionality that those events can provide.

private void Change(object sender, ChangeEventArgs args)
       
{
           
IObjectId oid = context.GetObjectId(args.PersistentObject);            
           
log=log+"\n<Changing oid='" + oid.ToString() +""+
                              "' field='"
+ args.FieldName +""+
                              "' oldValue='"
+ args.OldValue +""+
                              "' newValue='"
+ args.NewValue +""+
                              "' />"
;
       
}
       
private void Add(object sender, AddEventArgs args)
       
{
           
IObjectId oid = context.GetObjectId(args.PersistentObject);
                      
log = log + "\n<Added oid='" + oid.ToString() + "'/>";
       
}
       
private void Remove(object sender, RemoveEventArgs args)
       
{
           
IObjectId oid = context.GetObjectId(args.PersistentObject);
           
log = log + "\n<Removing oid='" + oid.ToString() + "' />";
       
}
       
private void Refresh(object sender, RefreshEventArgs args)
       
{
          
           
IObjectId oid = context.GetObjectId(args.PersistentObject);
           
log = log + "\n<Refreshing oid='" + oid.ToString() + "' />";
       
}
       
public void FlushToConsole()
       
{
           
Console.WriteLine("Messages so far:");
           
Console.WriteLine(log);
           
log = "";
       
}
public void WriteToTextFile(string path)
       
{
           
StreamWriter sw;
           
sw = File.AppendText(path);
           
sw.WriteLine(log);
           
log = "";
           
sw.Close();
       
}

Having this class implemented,we can easily use it as follows:

IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope();
           
string log = "";
           
Tracker tr = new Tracker(scope, log);
           
tr.Start();
           
scope.Transaction.Begin();
       
//some logic here
scope.Transaction.Commit();
           
tr.Stop();

Now we can switch the ObjectScope tracking on and off whenever we need it.

As you can see tracking changes using the ObjectScope events is simple. A lot of powerfull features can be unlocked with just a simple subscribition to the right events.

Stay tuned for more from the Telerik OpenAccess ORM team.


Comments

Comments are disabled in preview mode.