<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Advanced Features|Callbacks" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Event Registry API</title> <script type="text/javascript">/* <![CDATA[ */ window.onload = function(){ var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf'; ZeroClipboard.setMoviePath(pathToFlash); function bindToClipBord(element,content){ var clip = new ZeroClipboard.Client(); clip.setText(content); clip.glue(element); }; if(location.protocol==='file:'){ $('.copylink-marker').remove(); } else{ $('.copylink-marker').each(function(){ var text = $(this).parent().parent().children('.prettyprint').html(); $(this).hover(function(){ bindToClipBord(this,text); }, function(){}); }); } prettyPrint(); }; /* ]]> */</script> <link href="../../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" /> <script src="../../Resources/Code/prettify.js"> </script> <script src="../../Resources/Code/lang-vb.js"> </script> <script src="../../Resources/Code/jquery.min.js"> </script> <script src="../../Resources/Code/ZeroClipboard.js"> </script> <script src="../../SkinSupport/MadCapAll.js" type="text/javascript"> </script> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../../index_CSH.html#advanced_topics/callbacks/event_registry_api.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../../advanced_topics.htm">Advanced Features</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../callbacks.htm">Callbacks</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Event Registry API</span> </div> <p> <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">'); document.write("Direct Link"); document.write('</a>');/*]]>*/</script> </p> <p> </p> <h1><a name="kanchor81"></a>Event Registry API</h1> <p>You can register to events of the db4o-database. You can used these events to implement all kinds of additional functionality. Take a look a few example use-cases. <a href="possible_usecases.htm" target="" title="" alt="" class="MCXref" xrefformat="See "{paratext}"">See "Possible Usecases"</a></p> <p>There's an event for each database operation. Most of the time there are two events for an operation. One is fired before the operation starts, the other when the operation ends.</p> <h2>Register to an Event</h2> <p>You can gain access to the events via a event registry. These three steps show how to register to events.</p> <p>First obtain a <span class="PrimaryEventRegistry">IEventRegistry</span>-instance from the object container.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IEventRegistry events = EventRegistryFactory.ForObjectContainer(container);</pre> <div class="codesnippet-meta">EventRegistryExamples.cs: Obtain the event-registry <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim events As IEventRegistry = EventRegistryFactory.ForObjectContainer(container)</pre> <div class="codesnippet-meta">EventRegistryExamples.vb: Obtain the event-registry <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <p>Now you can register your event-handlers on the event registry.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">events.Committing += HandleCommitting;</pre> <div class="codesnippet-meta">EventRegistryExamples.cs: register for a event <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">AddHandler events.Committing, AddressOf HandleCommitting</pre> <div class="codesnippet-meta">EventRegistryExamples.vb: register for a event <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <p>Then implement your event handling.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">private static void HandleCommitting(object sender, CommitEventArgs commitEventArgs) { // handle the event } </pre> <div class="codesnippet-meta">EventRegistryExamples.cs: implement your event handling <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Private Shared Sub HandleCommitting(ByVal sender As Object, _ ByVal commitEventArgs As CommitEventArgs) ' handle the event End Sub</pre> <div class="codesnippet-meta">EventRegistryExamples.vb: implement your event handling <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <h2>Cancelable Events</h2> <p>Some events can cancel the operation. All events which have a CancellableObjectEventArgs-parameter can cancel the operation. When you cancel in a event, the operation won't be executed. For example:</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IEventRegistry events = EventRegistryFactory.ForObjectContainer(container); events.Creating += delegate(object sender, CancellableObjectEventArgs args) { if (args.Object is Person) { Person p = (Person) args.Object; if (p.Name.Equals("Joe Junior")) { args.Cancel(); } } }; </pre> <div class="codesnippet-meta">EventRegistryExamples.cs: Cancel store operation <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim events As IEventRegistry = EventRegistryFactory.ForObjectContainer(container) AddHandler events.Creating, AddressOf HandleCreatingEvent</pre> <div class="codesnippet-meta">EventRegistryExamples.vb: Cancel store operation <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Private Shared Sub HandleCreatingEvent(ByVal sender As Object, _ ByVal args As CancellableObjectEventArgs) If TypeOf args.[Object] Is Person Then Dim p As Person = DirectCast(args.[Object], Person) If p.Name.Equals("Joe Junior") Then args.Cancel() End If End If End Sub</pre> <div class="codesnippet-meta">EventRegistryExamples.vb: Cancel store operation Handler <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <h2>Register Events On The Server</h2> <p>When you want to register for the events on the server, you should register it on the server-container. </p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IObjectServer server = Db4oClientServer.OpenServer(DatabaseFileName, PortNumber); IEventRegistry eventsOnServer = EventRegistryFactory.ForObjectContainer(server.Ext().ObjectContainer());</pre> <div class="codesnippet-meta">EventRegistryExamples.cs: register for events on the server <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim server As IObjectServer = _ Db4oClientServer.OpenServer(DatabaseFileName, PortNumber) Dim eventsOnServer As IEventRegistry = _ EventRegistryFactory.ForObjectContainer(server.Ext().ObjectContainer())</pre> <div class="codesnippet-meta">EventRegistryExamples.vb: register for events on the server <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <h2>Commit-Events</h2> <p>Commit-events bring a collection of the added, updated and deleted object with it. You can iterate over these objects. The updated- and added-collections contain LazyObjectReferences, the deleted-event a FrozenObjectInfos. Note that you may cannot get deleted object-instance anymore, but only the meta-info. Furthermore the object doesn't need to be activated. So when you need to read information out if it, ensure that you've activated it first.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IEventRegistry events = EventRegistryFactory.ForObjectContainer(container); events.Committed += delegate(object sender, CommitEventArgs args) { foreach (LazyObjectReference reference in args.Added) { Console.WriteLine("Added " + reference.GetObject()); } foreach (LazyObjectReference reference in args.Updated) { Console.WriteLine("Updated " + reference.GetObject()); } foreach (FrozenObjectInfo reference in args.Deleted) { //the deleted info might doesn't contain the object anymore and //return the null. Console.WriteLine("Deleted " + reference.GetObject()); } };</pre> <div class="codesnippet-meta">EventRegistryExamples.cs: Commit-info <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim events As IEventRegistry = EventRegistryFactory.ForObjectContainer(container) AddHandler events.Committed, AddressOf HandlingCommitEvent</pre> <div class="codesnippet-meta">EventRegistryExamples.vb: Commit-info <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Private Shared Sub HandlingCommitEvent(ByVal sender As Object, ByVal args As CommitEventArgs) For Each reference As LazyObjectReference In args.Added Console.WriteLine("Added " & reference.GetObject()) Next For Each reference As LazyObjectReference In args.Updated Console.WriteLine("Updated " & reference.GetObject()) Next For Each reference As FrozenObjectInfo In args.Deleted 'the deleted info might doesn't contain the object anymore and 'return the null. Console.WriteLine("Deleted " & reference.GetObject()) Next End Sub</pre> <div class="codesnippet-meta">EventRegistryExamples.vb: Commit-info Handler <div class="codedownload"><a href="../../CodeExamples/callbacks/eventregistry/Example-callbacks-eventregistry-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <h2>Pitfalls and Limitations</h2> <ul> <li value="1">All embedded clients-/session share the same event registry. So you need to register the events only on one.</li> <li value="2">You cannot call recursively the event-producing operation within the event-handler. For example in the storing-event you cannot call store. In the committing-event you cannot call commit.</li> <li value="3">In client-server mode, each client has it's own event-registry, and therefore only sees its own events. Except the committed-event. <a href="../../client-server/events_in_cs_mode.htm" target="" title="" alt="" class="MCXref" xrefformat="See "{paratext}"">See "Events In Client Server-Mode"</a></li> </ul> <script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>