<?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|Possible Usecases" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Referential Integrity</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/possible_usecases/referential_integrity.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><a class="MCBreadcrumbsLink" href="../possible_usecases.htm">Possible Usecases</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Referential Integrity</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>Referential Integrity</h1> <p>db4o does not have a built-in referential integrity checking mechanism. Luckily EventRegistry gives you access to all the necessary events to implement it. You will just need to trigger validation on create, update or delete and cancel the action if the integrity is going to be broken.</p> <p>For example, if Car object is referencing Pilot and the referenced object should exist, this can be ensured with the following handler in deleting() event:</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">private static void ReferentialIntegrityCheck(object sender, CancellableObjectEventArgs eventArguments) { Object toDelete = eventArguments.Object; if (toDelete is Pilot) { IObjectContainer container = eventArguments.ObjectContainer(); IEnumerable<Car> cars = from Car c in container where c.Pilot == toDelete select c; if (cars.Count() > 0) { eventArguments.Cancel(); } } }</pre> <div class="codesnippet-meta">CallbackExamples.cs: Referential integrity <div class="codedownload"><a href="../../../CodeExamples/callbacks/examples/Example-callbacks-examples-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 ReferentialIntegrityCheck(ByVal sender As Object, ByVal eventArguments As CancellableObjectEventArgs) Dim toDelete As Object = eventArguments.Object If TypeOf toDelete Is Pilot Then Dim container As IObjectContainer = eventArguments.ObjectContainer() Dim cars As IEnumerable(Of Car) = From c As Car In container _ Where c.Pilot Is toDelete If cars.Count() > 0 Then eventArguments.Cancel() End If End If End Sub</pre> <div class="codesnippet-meta">CallbackExamples.vb: Referential integrity <div class="codedownload"><a href="../../../CodeExamples/callbacks/examples/Example-callbacks-examples-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.c#"> <pre class="prettyprint" xml:space="preserve">IEventRegistry events = EventRegistryFactory.ForObjectContainer(container); events.Deleting += ReferentialIntegrityCheck;</pre> <div class="codesnippet-meta">CallbackExamples.cs: Register handler <div class="codedownload"><a href="../../../CodeExamples/callbacks/examples/Example-callbacks-examples-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.Deleting, AddressOf ReferentialIntegrityCheck</pre> <div class="codesnippet-meta">CallbackExamples.vb: Register handler <div class="codedownload"><a href="../../../CodeExamples/callbacks/examples/Example-callbacks-examples-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>You can also add handlers for creating() and updating() events for a Car object to make sure that the pilot field is not null. </p> <p>Note, that in client/server mode deleting event is only raised on the server side, therefore the code above can't be used and will throw an exception. </p> <script type="text/javascript" src="../../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>