Custom Marshaller Implementation

Let's implement a marshaller for a simple class and check its influence on the performance.

We will marshal the following class:

Item.cs
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02using System; 03 04namespace Db4objects.Db4odoc.Marshal 05{ 06 class Item 07 { 08 public int _one; 09 10 public long _two; 11 12 public int _three; 13 14 public Item(int one, long two, int three) { 15 _one = one; 16 _two = two; 17 _three = three; 18 19 } 20 21 public Item() { 22 23 } 24 25 public override string ToString() { 26 return String.Format("{0:X}, {1:X}, {2:N}", _one, _two, _three); 27 } 28 } 29}

Item.vb
01' Copyright (C) 2007 db4objects Inc. http://www.db4o.com 02Namespace Db4objects.Db4odoc.marshal 03 04 Class Item 05 Public _one As Integer 06 Public _two As Long 07 Public _three As Integer 08 09 Public Sub New(ByVal one As Integer, ByVal two As Long, ByVal three As Integer) 10 _one = one 11 _two = two 12 _three = three 13 End Sub 14 15 Public Sub New() 16 End Sub 17 18 Public Overloads Overrides Function ToString() As String 19 Return String.Format("{0:X}, {1:X}, {2:N}", _one, _two, _three) 20 End Function 21 End Class 22End Namespace

The marshaller - ItemMarshaller - will need to implement writeFields, readFields  and marshalledFieldLength methods.

As Item class has only int and long fields we can use PrimitiveCodec class to write the fields to the byte array:

ItemMarshaller.cs
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02using Db4objects.Db4o.Config; 03using Db4objects.Db4o.Foundation; 04 05namespace Db4objects.Db4odoc.Marshal 06{ 07 class ItemMarshaller: IObjectMarshaller 08 { 09 // Write field values to a byte array 10 // No reflection is used 11 public void WriteFields(object obj, byte[] slot, int offset) { 12 Item item = (Item)obj; 13 PrimitiveCodec.WriteInt(slot, offset, item._one); 14 offset+= PrimitiveCodec.INT_LENGTH; 15 PrimitiveCodec.WriteLong(slot, offset, item._two); 16 offset+= PrimitiveCodec.LONG_LENGTH; 17 PrimitiveCodec.WriteInt(slot, offset, item._three); 18 } 19 20 // Restore field values from the byte array 21 // No reflection is used 22 public void ReadFields(object obj, byte[] slot, int offset) { 23 Item item = (Item)obj; 24 item._one = PrimitiveCodec.ReadInt(slot, offset); 25 offset+= PrimitiveCodec.INT_LENGTH; 26 item._two = PrimitiveCodec.ReadLong(slot, offset); 27 offset+= PrimitiveCodec.LONG_LENGTH; 28 item._three = PrimitiveCodec.ReadInt(slot, offset); 29 } 30 31 public int MarshalledFieldLength() { 32 return PrimitiveCodec.INT_LENGTH * 2 + PrimitiveCodec.LONG_LENGTH; 33 } 34 } 35}

ItemMarshaller.vb
01' Copyright (C) 2007 db4objects Inc. http://www.db4o.com 02Imports Db4objects.Db4o.Config 03Imports Db4objects.Db4o.Foundation 04Namespace Db4objects.Db4odoc.marshal 05 06 Class ItemMarshaller 07 Implements IObjectMarshaller 08 09 ' Write field values to a byte array 10 ' No reflection is used 11 Public Sub WriteFields(ByVal obj As Object, ByVal slot As Byte(), ByVal offset As Integer) Implements IObjectMarshaller.WriteFields 12 Dim item As Item = CType(obj, Item) 13 PrimitiveCodec.WriteInt(slot, offset, item._one) 14 offset += PrimitiveCodec.INT_LENGTH 15 PrimitiveCodec.WriteLong(slot, offset, item._two) 16 offset += PrimitiveCodec.LONG_LENGTH 17 PrimitiveCodec.WriteInt(slot, offset, item._three) 18 End Sub 19 ' end WriteFields 20 21 ' Restore field values from the byte array 22 ' No reflection is used 23 Public Sub ReadFields(ByVal obj As Object, ByVal slot As Byte(), ByVal offset As Integer) Implements IObjectMarshaller.ReadFields 24 Dim item As Item = CType(obj, Item) 25 item._one = PrimitiveCodec.ReadInt(slot, offset) 26 offset += PrimitiveCodec.INT_LENGTH 27 item._two = PrimitiveCodec.ReadLong(slot, offset) 28 offset += PrimitiveCodec.LONG_LENGTH 29 item._three = PrimitiveCodec.ReadInt(slot, offset) 30 End Sub 31 ' end ReadFields 32 33 Public Function MarshalledFieldLength() As Integer Implements IObjectMarshaller.MarshalledFieldLength 34 Return PrimitiveCodec.INT_LENGTH * 2 + PrimitiveCodec.LONG_LENGTH 35 End Function 36 ' end MarshalledFieldLength 37 38 End Class 39End Namespace