<?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="db4o Databases Replication" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Selective Replication</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#db4o_replication_system_drs/db4o_databases_replication/selective_replication.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../db4o_databases_replication.htm">db4o Databases Replication</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Selective Replication</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="kanchor5"></a>Selective Replication</h1> <p>What if the mobile device doesn't have enough memory to store all objects? Or if only a subset of the object need to be in both databases? In this case you can replicate only a subset of all objects.</p> <h2>Filter by Class</h2> <p>You can ask the dRS to replicate only the objects of a certain class. Instead of getting the change-set with all objects, it will return only the changed objects of that class.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IObjectSet changesOnDesktop = replicationSession.ProviderA().ObjectsChangedSinceLastReplication(typeof (Pilot)); foreach (object changedObjectOnDesktop in changesOnDesktop) { replicationSession.Replicate(changedObjectOnDesktop); } replicationSession.Commit();</pre> <div class="codesnippet-meta">Db4oReplicationExamples.cs: Selective replication by class <div class="codedownload"><a href="../../CodeExamples/drs/db4o/Example-drs-db4o-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 changesOnDesktop As IObjectSet = _ replicationSession.ProviderA().ObjectsChangedSinceLastReplication(GetType(Pilot)) For Each changedObjectOnDesktop As Object In changesOnDesktop replicationSession.Replicate(changedObjectOnDesktop) Next replicationSession.Commit()</pre> <div class="codesnippet-meta">Db4oReplicationExamples.vb: Selective replication by class <div class="codedownload"><a href="../../CodeExamples/drs/db4o/Example-drs-db4o-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>Filter With a Condition</h2> <p>For more complex cases a simple condition can be a solution. Add a simple condition within the replication loop to select which objects are replicated and which ones not.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IObjectSet changesOnDesktop = replicationSession.ProviderA().ObjectsChangedSinceLastReplication(); foreach (object changedObjectOnDesktop in changesOnDesktop) { if (changedObjectOnDesktop is Car) { if (((Car) changedObjectOnDesktop).Name.StartsWith("M")) { replicationSession.Replicate(changedObjectOnDesktop); } } } replicationSession.Commit();</pre> <div class="codesnippet-meta">Db4oReplicationExamples.cs: Selective replication with a condition <div class="codedownload"><a href="../../CodeExamples/drs/db4o/Example-drs-db4o-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 changesOnDesktop As IObjectSet = _ replicationSession.ProviderA().ObjectsChangedSinceLastReplication() For Each changedObjectOnDesktop As Object In changesOnDesktop If TypeOf changedObjectOnDesktop Is Car Then If DirectCast(changedObjectOnDesktop, Car).Name.StartsWith("M") Then replicationSession.Replicate(changedObjectOnDesktop) End If End If Next replicationSession.Commit()</pre> <div class="codesnippet-meta">Db4oReplicationExamples.vb: Selective replication with a condition <div class="codedownload"><a href="../../CodeExamples/drs/db4o/Example-drs-db4o-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>Filter With a Query</h2> <p>It's also possible to query for the objects and the replicate those objects. However in this case, also unchanged objects are replicated again.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IList<Car> changesOnDesktop = desktopDatabase.Query(delegate(Car car) { return car.Name.StartsWith("M"); }); foreach (Car changedObjectOnDesktop in changesOnDesktop) { replicationSession.Replicate(changedObjectOnDesktop); } replicationSession.Commit();</pre> <div class="codesnippet-meta">Db4oReplicationExamples.cs: Selective replication with a query <div class="codedownload"><a href="../../CodeExamples/drs/db4o/Example-drs-db4o-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 changesOnDesktop As IList(Of Car) = _ desktopDatabase.Query(Function(car As Car) car.Name.StartsWith("M")) For Each changedObjectOnDesktop As Car In changesOnDesktop replicationSession.Replicate(changedObjectOnDesktop) Next replicationSession.Commit()</pre> <div class="codesnippet-meta">Db4oReplicationExamples.vb: Selective replication with a query <div class="codedownload"><a href="../../CodeExamples/drs/db4o/Example-drs-db4o-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>Combination</h2> <p>Of course it possible to use a combination of these three possibilities. For example you can request the changes for a certain class and then filter with a condition.</p> <p MadCap:conditions="Primary..NET"> The IObjectSet is also an IEnumerable, so you can use LINQ to filter the objects.</p> <h2 MadCap:conditions="Primary..NET">Consistency with Selective Replication</h2> <p MadCap:conditions="Primary..NET">Note that dRS tries to avoid inconsistent replications. When replicating a object all its referenced objects are replicated as well. This avoids a incomplete and inconsistent object graphs. So for example when you do a selective replication of the Car objects, dRS will also replicate all Pilots which are referenced by the Car objects.</p> <p MadCap:conditions="Primary..NET"> </p> <script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>