<html> <head> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Event Registry API</title> <link rel="stylesheet" type="text/css" href="../../../style.css"> </head> <body> <div class="CommonContent"> <div class="CommonContentArea"> <h1>Event Registry API</h1><script> //We attach this function to a browser object as a variable so that if multiple of these blocks are //present on the same page, then they will simply over-write eachother with the same function instead //of generating a naming collision. window.WikiCodeFormattingCopyToClipboard = function copyToClipboard(sID){ var sContent = document.getElementById(sID).innerText; if( window.clipboardData && clipboardData.setData ) { clipboardData.setData("Text", sContent); } else { alert("You must enable javascript access to your clipboard for this feature to work. Please referr to your browser documentation or Google search for instructions."); } } </script> <p>External callbacks should be registered with db4o EventRegistry. Follow the steps below to start using your own event handlers:</p> <ol> <li>Obtain an instance of EventRegistry object for your ObjectContainer </p> <span name="cs_wiki_filter" csw_filters="cs"> <p>c#:</p> <p><code>IEventRegistry registry = EventRegistryFactory.ForObjectContainer(container);</code> </p> </span> <span name="cs_wiki_filter" csw_filters="cs"> <p>VB:</p> <p><code>Dim registry As IEventRegistry = EventRegistryFactory.ForObjectContainer(container)</code></p> </span> </li> <li>Register the required event. For "created" event the code is the following: <span name="cs_wiki_filter" csw_filters="cs"> <p>c#:</p> <p><code>registry.Created += new ObjectEventHandler(OnCreated);</code></p> </span> <span name="cs_wiki_filter" csw_filters="vb"> <p>VB:</p> <p><code>AddHandler registry.Created, AddressOf OnCreated</code></p> </span> </li> <li>Create your own event handler: <span name="cs_wiki_filter" csw_filters="cs"> <p>c#: </p> <p><code>private static void OnCreated(object sender, ObjectEventArgs args)<br> {<br> // handling code <br> }</code></p> </span> <span name="cs_wiki_filter" csw_filters="vb"> <p>VB: </p> <p><code>Private Shared Sub OnCreated(ByVal sender As Object, ByVal args As ObjectEventArgs)<br> ' handling code<br>End Sub</code></p> </span> <p>The action raised the event can be cancelled in Creating, Activating, Deactivating, Updating and Deleting event handlers. These events accept CancellableObjectEventArgs as a parameter. In order to cancel the action use:</p> <span name="cs_wiki_filter" csw_filters="cs"> <p>c#:<br></p> <p><code>cancellableEventArgs.Cancel()</code></p> </span> <span name="cs_wiki_filter" csw_filters="vb"> <p>VB:</p> <p><code>cancellableEventArgs.Cancel()</code></p> </span> <p>Here <code>cancellableEventArgs</code> is an event argument of <code>CancellableObjectEventArgs</code> type.</p> <span name="cs_wiki_filter" csw_filters="cs"> <p>c#:</p> <p><code>registry.Created -= new ObjectEventHandler(OnCreated);</code></p> </span> <span name="cs_wiki_filter" csw_filters="vb"> <p>VB:</p> <p><code>RemoveHandler registry.Created, AddressOf OnCreated</code></p> </span> </li> </ol> <p>EventRegistry features:</p> <ul> <li>You can register several event handlers for a single event. </li> <li>You can get different EventRegistry's for different ObjectContainer instances using EventRegistryFactory. </li> <li>In Java, callbacks are implemented as Listeners, .NET uses Native events </li> <li>Callbacks only work run in local mode or on the server side in client/server mode. </li> <li>Each event applies to all the objects or queries(QueryStarted/QueryFinished events ). In order to distinguish the specific case, to which the handler should be applied, use the event arguments. </li> </ul> <p>For example:</p> <span name="cs_wiki_filter" csw_filters="cs"> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">CallbacksExample.cs: <span class="fscMemberName">TestCreated</span></span></div><div class="fscCode"><pre ID="ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">01</span><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/None.gif" align="top"/><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> TestCreated() </span><span style="color: #008080;">02</span><span style="color: #000000;"><img id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_42_660_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_42_660_Open_Text').style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_42_660_Closed_Image').style.display='inline'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_42_660_Closed_Text').style.display='inline';"/><img id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_42_660_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_42_660_Closed_Text').style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_42_660_Open_Image').style.display='inline'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_42_660_Open_Text').style.display='inline';"/> </span><span id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_42_660_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_42_660_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">03</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> File.Delete(Db4oFileName); </span><span style="color: #008080;">04</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IObjectContainer container </span><span style="color: #000000;">=</span><span style="color: #000000;"> OpenContainer(); </span><span style="color: #008080;">05</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">try</span><span style="color: #000000;"> </span><span style="color: #008080;">06</span><span style="color: #000000;"><img id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_169_568_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_169_568_Open_Text').style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_169_568_Closed_Image').style.display='inline'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_169_568_Closed_Text').style.display='inline';"/><img id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_169_568_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedSubBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_169_568_Closed_Text').style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_169_568_Open_Image').style.display='inline'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_169_568_Open_Text').style.display='inline';"/> </span><span id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_169_568_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_169_568_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">07</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IEventRegistry registry </span><span style="color: #000000;">=</span><span style="color: #000000;"> EventRegistryFactory.ForObjectContainer(container); </span><span style="color: #008080;">08</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #008000;">//</span><span style="color: #008000;"> register an event handler, which will print all the car objects, that have been Created</span><span style="color: #008000;"> </span><span style="color: #008080;">09</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/></span><span style="color: #000000;"> registry.Created </span><span style="color: #000000;">+=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> ObjectEventHandler(OnCreated); </span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> Car car </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Car(</span><span style="color: #800000;">"</span><span style="color: #800000;">BMW</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Pilot(</span><span style="color: #800000;">"</span><span style="color: #800000;">Rubens Barrichello</span><span style="color: #800000;">"</span><span style="color: #000000;">)); </span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> container.Set(car); </span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"/> }</span></span><span style="color: #000000;"> </span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">finally</span><span style="color: #000000;"> </span><span style="color: #008080;">15</span><span style="color: #000000;"><img id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_601_650_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_601_650_Open_Text').style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_601_650_Closed_Image').style.display='inline'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_601_650_Closed_Text').style.display='inline';"/><img id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_601_650_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedSubBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_601_650_Closed_Text').style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_601_650_Open_Image').style.display='inline'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_601_650_Open_Text').style.display='inline';"/> </span><span id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_601_650_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksCS.ZipsCallbacksExample.Cs_CallbacksExample.Cs_TestCreated_3301_61_601_650_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">16</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> CloseContainer(); </span><span style="color: #008080;">17</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"/> }</span></span><span style="color: #000000;"> </span><span style="color: #008080;">18</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> }</span></span></div></pre></div></div> </span> <span name="cs_wiki_filter" csw_filters="vb"> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">CallbacksExample.vb: <span class="fscMemberName">TestCreated</span></span></div><div class="fscCode"><pre ID="ResourcesReferenceImplementation_StrategiesCallbackscallbacksVB.ZipsCallbacksExample.Vb_CallbacksExample.Vb_TestCreated_3428_61"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">01</span><img id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksVB.ZipsCallbacksExample.Vb_CallbacksExample.Vb_TestCreated_3428_61_15_624_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksVB.ZipsCallbacksExample.Vb_CallbacksExample.Vb_TestCreated_3428_61_15_624_Open_Text').style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksVB.ZipsCallbacksExample.Vb_CallbacksExample.Vb_TestCreated_3428_61_15_624_Closed_Image').style.display='inline'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksVB.ZipsCallbacksExample.Vb_CallbacksExample.Vb_TestCreated_3428_61_15_624_Closed_Text').style.display='inline';"/><img id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksVB.ZipsCallbacksExample.Vb_CallbacksExample.Vb_TestCreated_3428_61_15_624_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksVB.ZipsCallbacksExample.Vb_CallbacksExample.Vb_TestCreated_3428_61_15_624_Closed_Text').style.display='none'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksVB.ZipsCallbacksExample.Vb_CallbacksExample.Vb_TestCreated_3428_61_15_624_Open_Image').style.display='inline'; document.getElementById('ResourcesReferenceImplementation_StrategiesCallbackscallbacksVB.ZipsCallbacksExample.Vb_CallbacksExample.Vb_TestCreated_3428_61_15_624_Open_Text').style.display='inline';"/><span style="color: #0000FF;">Private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Shared</span><span style="color: #000000;"> </span><span id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksVB.ZipsCallbacksExample.Vb_CallbacksExample.Vb_TestCreated_3428_61_15_624_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">Sub TestCreated()</span><span id="ResourcesReferenceImplementation_StrategiesCallbackscallbacksVB.ZipsCallbacksExample.Vb_CallbacksExample.Vb_TestCreated_3428_61_15_624_Open_Text"><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> TestCreated() </span><span style="color: #008080;">02</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> File.Delete(Db4oFileName) </span><span style="color: #008080;">03</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> container </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IObjectContainer </span><span style="color: #000000;">=</span><span style="color: #000000;"> OpenContainer </span><span style="color: #008080;">04</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Try</span><span style="color: #000000;"> </span><span style="color: #008080;">05</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> registry </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IEventRegistry </span><span style="color: #000000;">=</span><span style="color: #000000;"> EventRegistryFactory.ForObjectContainer(container) </span><span style="color: #008080;">06</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #008000;">'</span><span style="color: #008000;"> register an event handler, which will print all the car objects, that have been Created</span><span style="color: #008000;"> </span><span style="color: #008080;">07</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/></span><span style="color: #000000;"> </span><span style="color: #0000FF;">AddHandler</span><span style="color: #000000;"> registry.Created, </span><span style="color: #0000FF;">AddressOf</span><span style="color: #000000;"> OnCreated </span><span style="color: #008080;">08</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> car </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> Car </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> Car(</span><span style="color: #800000;">"</span><span style="color: #800000;">BMW</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> Pilot(</span><span style="color: #800000;">"</span><span style="color: #800000;">Rubens Barrichello</span><span style="color: #800000;">"</span><span style="color: #000000;">)) </span><span style="color: #008080;">09</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> container.Set(car) </span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Finally</span><span style="color: #000000;"> </span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> CloseContainer() </span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Try</span><span style="color: #000000;"> </span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> </span><span style="color: #0000FF;">End Sub</span></span></div></pre></div></div> </span></div> </div> <div id="footer"> This revision (11) was last Modified 2007-04-28T09:39:20 by Tetyana. </div> </body> </html>