Archive for the ‘Uncategorized’ Category

You can do crazy things with WCF

Friday, March 6th, 2009

Have you ever wanted to code something that wasn’t exactly best practices, but would make for a neat concept?  I know that I have.  I seem to be constantly pushing against the edges of best practices hoping to find the next “Dependency Injection” concept that makes everyones lives just a little bit easier.  While my current spike project is not of that caliber, it is pushing the boundaries in a weird sort of way.  So instead of just laughing at myself, I thought that I’d open up access to it and let you guys laugh at me as well.  So what is this spike that I’m talking about?  This is a database driver for NHibernate that works accross the network, or across WCF specifically.

The concept is very simple.  NHibernate, at its very base relies on an IDbConnection to go out and fetch the data from the database.  Normally this connects rather directly to the db in a low latency connection.  Being an intensely curious programmer I wondered if it would be possible to route this raw db connection over top of a WCF connection.  The cool things is that it works, although introducing more latency than is optimal of course.  It works by opening up the actual db connection on the server and then reading the data associated with a command into a dataset which is transmitted accross WCF.  On the client side a datareader is created from the dataset thus allowing the client to read the database and perform other operations.

How reliable is this concept.  Well I haven’t had the chance to use it in any production apps.  (Not sure that I’d want to) but it does pass all of NHibernate 2.0.1GA’s test save one that deals with schemas. Let’s say that this piques your interest a little bit and you would like to try it out.  I’m not going to be releasing binaries and a big how to, but here’s the SVN url and you can give it a try for yourselves.

There is a project in the source called “TestServer” which is simply a console project that will host the server.  The server is the only part that has a custom configuration element in the app.config.  Both the server and the client rely on standard WCF configuration to create a connection.  You can find examples of this configuration in both the app.config of the NHibernate.Test-2.0 project and the app.config of the TestServer project.  It’s also important to note that when you are using the WCFDbDriver on the client, the NHibernate connection string setting is ignored.  You can put whatever you want there but you will have to give it a value or NHibernate will complain.  Actual client-server connection information is located in the WCF configuration elements.

This is definitely not in keeping with best-practices but who says you can’t have fun with programming sometimes.