Transferring .net object graphs using RemObjects SDK 4.0

by Miha Markič 25. October 2006 21:03

If you happen to use RemObjects SDK 4.0 (RO) for an alternative to remoting (I'll discuss the benefits in another post) than you are certainly facing one problem. RO thing doesn't support .net classes as method arguments in favor of interoperability (you can't use .net object graph from a different world, i.e. Delphi, right) - out of the box it supports only common value types (ints, strings, etc.) with some degree of extensibility. This isn't good when you want to push a .net object graph over the wire of course. Transferring .net object graphs works only in .net environment and it is not interoperable with other worlds. So as long as you are doing .net - .net you are fine.

The best option for this scenario is RO's Binary data type, a class derived from MemoryStream in RO.net version. I guess you see where I am going - I'll be doing manual (de)serialization to/from Binary.

Imagine we have a RO service method defined like this:

public Binary DoSomething(Binary args) { ... }

and in reality it should look like this:

public ReturnClass DoSomething(SomeClass args) { ... }

You will have to do some classic serialization:

public ReturnClass DoSomething(SomeClass args) { Binary binaryIn = new Binary(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(binaryIn, data); binaryIn.Position = 0; Binary result = DoSomething(binaryIn);

and some classic deserialization:

result.Position = 0; BinaryFormatter formatter = new BinaryFormatter(); return (ReturnClass)formatter.Deserialize(result); }

However, when you deal with many methods that require this exercise the task soon becomes annoying. That's why I've created ROHelper class that does much of the (de)serialization for you. With some help of generics here it is:

 

public class ROHelper { public static T ConvertFromBinary<T>(Binary binary) { binary.Position = 0; BinaryFormatter formatter = new BinaryFormatter(); return (T)formatter.Deserialize(binary); } public static Binary ConvertFrom<T>(T data) { Binary binary = new Binary(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(binary, data); binary.Position = 0; return binary; } }

and here is the DoSomething method rewritten:

public ReturnClass DoSomething(SomeClass args) { return ROHelper.ConvertFromBinary<ResultClass>( DoSomething( ROHelper.ConvertFrom(args) )); }

Isn't this code much better than spaghetti code above? Also note, that method ConvertFrom<T> doesn't require explicit definition of type T as it is deduced from argument itself.

The only thing that remains to improve is code generation itself. Even with simpler code like this you have to write all those method wrappers. So, the next step is to build code wrapper generator. Next time, perhaps.

Tags:

.net

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Miha Markic

About me
Righthand
 
Microsoft MVP
 
Developer Express' DXSquad
INETA Country Leader for Slovenia
INETA Country Leader for Slovenia

Slovene Developer Users Group Lead
Friends of Red-Gate
LLBLGenPro Partner

Miha currently works as a free lance consultant and software developer specialized in .net area.
He graduated in Computer and information science at the University of Ljubljana, Slovenia. He has accumulated experience in various programming languages such as Java, Visual Basic 3-6 (MCP), Visual C++, Delphi, C# and VB.Net through years.
He has experience in practically all (technical) stages of project development, including planning, framework development, user interface, business processes, as well as testing and documenting. He has worked on big and small projects in Slovenia and abroad (e.g. participated in completing level 3 IS for the Nucor steel plant, Hertford, USA).
Currently he enjoys programming in .net environment using C#. Since 2000 he has been active in Developer Express' DX Squad and has been ECDL trainer and tester. He also gives lectures on conferences and other events in Slovenia.

Month List

Tag cloud

Most comments

Dan Dan
4 comments
ca Canada
Thomas Thomas
3 comments
de Germany
Sebastian Sebastian
1 comments
ca Canada

RecentComments

Comment RSS