<?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="Platform Specific Issues|Disconnected Objects|Merging Changes" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Example Merge Changes</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#platform_specific_issues/disconnected_objects/example_merge_changes.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../../platform_specific_issues.htm">Platform Specific Issues</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../disconnected_objects.htm">Disconnected Objects</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="merging_changes.htm">Merging Changes</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Example Merge Changes</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>Example Merge Changes</h1> <p style="font-weight: normal;">This example shows how changes are merged from the disconnected object to the object to update. To do this, traverse the object-graph and copy all value types over. All reference-types are first checked if they're an existing object. If it is,the primitives are copied over, otherwise it's a stored as a new object.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">using (IObjectContainer container = OpenDatabase()) { // first get the object from the database Car carInDb = GetCarById(container, disconnectedCar.ObjectId); // copy the value-objects (int, long, double, string etc) carInDb.Name = disconnectedCar.Name; // traverse into the references Pilot pilotInDB = carInDb.Pilot; Pilot disconnectedPilot = disconnectedCar.Pilot; // check if the object is still the same if (pilotInDB.ObjectId.Equals(disconnectedPilot.ObjectId)) { // if it is, copy the value-objects pilotInDB.Name = disconnectedPilot.Name; pilotInDB.Points = disconnectedPilot.Points; } else { // otherwise replace the object carInDb.Pilot = disconnectedPilot; } // finally store the changes container.Store(pilotInDB); container.Store(carInDb);</pre> <div class="codesnippet-meta">MergeExample.cs: merging <div class="codedownload"><a href="../../CodeExamples/disconnectedobj/merging/Example-disconnectedobj-merging-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">Using container As IObjectContainer = OpenDatabase() ' first get the object from the database Dim carInDb As Car = GetCarById(container, disconnectedCar.ObjectId) ' copy the value-objects (int, long, double, string etc) carInDb.Name = disconnectedCar.Name ' traverse into the references Dim pilotInDB As Pilot = carInDb.Pilot Dim disconnectedPilot As Pilot = disconnectedCar.Pilot ' check if the object is still the same If pilotInDB.ObjectId.Equals(disconnectedPilot.ObjectId) Then ' if it is, copy the value-objects pilotInDB.Name = disconnectedPilot.Name pilotInDB.Points = disconnectedPilot.Points Else ' otherwise replace the object carInDb.Pilot = disconnectedPilot End If ' finally store the changes container.Store(pilotInDB)</pre> <div class="codesnippet-meta">MergeExample.vb: merging <div class="codedownload"><a href="../../CodeExamples/disconnectedobj/merging/Example-disconnectedobj-merging-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 use reflection to automated this process. You can also use existing libraries like <a href="http://automapper.codeplex.com/" MadCap:conditions="Primary..NET">Automapper</a> <![CDATA[ ]]> which help you to do this.</p> <script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>