Using the ObjectScope events

Posted by: the telerik blogs, on 20 Aug 2009 | View original | Bookmarked: 0 time(s)

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.

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: Data | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 953 | Hits: 9

Similar Posts

  • Fun with Func<T,TResult> Delegates, Events and Async Operations more
  • Creating Objects with Observable Properties in JavaScript more
  • A Lightweight Event Framework in JavaScript more
  • Tech Ed 2009 Cometh - EF and more more
  • Twitterjecting more
  • Engage the Customer at the Point-of-Sale Part II more
  • Upcoming SoCal Tech Events more
  • The DefaultButton Property - News To Me! more
  • CodeMash 2009 Aftermash more
  • Announcing ShadowCamp more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD