<?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|Type Handling|Typehandlers" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Custom Typehandler Example</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/type_handling/typehandlers/custom_typehandler_example.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="../../type_handling.htm">Type Handling</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../typehandlers.htm">Typehandlers</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Custom Typehandler Example</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>Custom Typehandler Example</h1> <p>For a custom typehandler example we will try to write a very simple typehandler for the StringBuilder class. We want to handle a StringBuilder as a value type, therefore we implement the ValueTypeHandler interface. Not that there's a whole collection of interfaces for typehandlers. Take a look at the TypeHandler4 type hierarchy. </p> <p>To keep it simple we will skip information required for indexing - please look at IndexableTypeHandler in db4o sources to get more information on how to handle indexes.</p> <p>The first thing should be the write method, which determines how the object is persisted:</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">public void Write(IWriteContext writeContext, object o) { StringBuilder builder = (StringBuilder) o; string str = builder.ToString(); byte[] bytes = Encoding.UTF8.GetBytes(str); writeContext.WriteInt(bytes.Length); writeContext.WriteBytes(bytes); }</pre> <div class="codesnippet-meta">StringBuilderHandler.cs: Write the StringBuilder <div class="codedownload"><a href="../../../CodeExamples/typehandling/typehandler/Example-typehandling-typehandler-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">Public Sub Write(ByVal writeContext As IWriteContext, ByVal o As Object) _ Implements IValueTypeHandler.Write Dim builder As StringBuilder = DirectCast(o, StringBuilder) Dim str As String = builder.ToString() Dim bytes As Byte() = Encoding.UTF8.GetBytes(str) writeContext.WriteInt(bytes.Length) writeContext.WriteBytes(bytes) End Sub</pre> <div class="codesnippet-meta">StringBuilderHandler.vb: Write the StringBuilder <div class="codedownload"><a href="../../../CodeExamples/typehandling/typehandler/Example-typehandling-typehandler-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>As you can see from the code above, there are 3 steps:</p> <ol> <li value="1">Get the buffer from WriteContext/I WriteContext</li> <li value="2">Convert the string-content to a byte-array using the UTF8 encoding.</li> <li value="3">Write the length of the resulted byte-array.</li> <li value="4">Write the byte array of the string.</li> </ol> <p>Next step is to read the stored object. It is just opposite to the write method:</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve"> public Object Read(IReadContext readContext) { int length = readContext.ReadInt(); byte[] data = new byte[length]; readContext.ReadBytes(data); return new StringBuilder(Encoding.UTF8.GetString(data)); } }</pre> <div class="codesnippet-meta">StringBuilderHandler.cs: Read the StringBuilder <div class="codedownload"><a href="../../../CodeExamples/typehandling/typehandler/Example-typehandling-typehandler-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">Public Function Read(ByVal readContext As IReadContext) As Object _ Implements IValueTypeHandler.Read Dim length As Integer = readContext.ReadInt() Dim data As Byte() = New Byte(length - 1) {} readContext.ReadBytes(data) Return New StringBuilder(Encoding.UTF8.GetString(data)) End Function</pre> <div class="codesnippet-meta">StringBuilderHandler.vb: Read the StringBuilder <div class="codedownload"><a href="../../../CodeExamples/typehandling/typehandler/Example-typehandling-typehandler-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>Delete is simple - we just reposition the buffer offset to the end of the slot:</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">public void Delete(IDeleteContext deleteContext) { SkipData(deleteContext); } private static void SkipData(IReadBuffer deleteContext) { int numBytes = deleteContext.ReadInt(); deleteContext.Seek(deleteContext.Offset() + numBytes); }</pre> <div class="codesnippet-meta">StringBuilderHandler.cs: Delete the content <div class="codedownload"><a href="../../../CodeExamples/typehandling/typehandler/Example-typehandling-typehandler-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">Public Sub Delete(ByVal deleteContext As IDeleteContext) _ Implements IValueTypeHandler.Delete SkipData(deleteContext) End Sub Private Shared Sub SkipData(ByVal deleteContext As IReadBuffer) Dim numBytes As Integer = deleteContext.ReadInt() deleteContext.Seek(deleteContext.Offset() + numBytes) End Sub</pre> <div class="codesnippet-meta">StringBuilderHandler.vb: Delete the content <div class="codedownload"><a href="../../../CodeExamples/typehandling/typehandler/Example-typehandling-typehandler-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>The last method left: #defragment. This one only moves the offset to the beginning of the object data, i.e. skips Id and size information (to be compatible to older versions):</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">public void Defragment(IDefragmentContext defragmentContext) { SkipData(defragmentContext); }</pre> <div class="codesnippet-meta">StringBuilderHandler.cs: Defragment the content <div class="codedownload"><a href="../../../CodeExamples/typehandling/typehandler/Example-typehandling-typehandler-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">Public Sub Defragment(ByVal defragmentContext As IDefragmentContext) _ Implements IValueTypeHandler.Defragment SkipData(defragmentContext) End Sub</pre> <div class="codesnippet-meta">StringBuilderHandler.vb: Defragment the content <div class="codedownload"><a href="../../../CodeExamples/typehandling/typehandler/Example-typehandling-typehandler-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 to use this type handler we need to configure db4o. To register a typehandler you have to provide a predicate which decides if a type is handled by the typehandler and the typehandler itself.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration(); configuration.Common.RegisterTypeHandler( new SingleClassTypeHandlerPredicate(typeof(StringBuilder)), new StringBuilderHandler());</pre> <div class="codesnippet-meta">TypeHandlerExample.cs: Register type handler <div class="codedownload"><a href="../../../CodeExamples/typehandling/typehandler/Example-typehandling-typehandler-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 configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration() configuration.Common.RegisterTypeHandler( New SingleClassTypeHandlerPredicate(GetType(StringBuilder)), New StringBuilderHandler())</pre> <div class="codesnippet-meta">TypeHandlerExample.vb: Register type handler <div class="codedownload"><a href="../../../CodeExamples/typehandling/typehandler/Example-typehandling-typehandler-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>After that all string builders are handled by you're type handler.</p> <script type="text/javascript" src="../../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>