Monday, February 06, 2012
 
   
 
Welcome to my site

First let me say thanks for stopping by my site. My name is David Hanson-Graville and I am a IT consultant working in the UK. Let me make it clear, I am passionate about technology and specifically .net and its various forms. I've programmed in a range of langages, but I can say, I am now at my happiest when coding with c#. I hope my blog is an enjoyable & educational read and please feel free to email me at David.Hanson@OnTheBlog.net if you have any questions. 

NHibernate and Tight Coupling Minimize
Location: BlogsOnTheBlog    
Posted by: David Hanson Wed, 05 Nov 2008 10:59:15 GMT

So, I'm half way through the week and had a chance to reflect on all the information I absorbed at the UK Alt.net conference last weekend. There were 4 sessions I attended during the day and the 3rd session was titled “NHibernate, Linq and ORM”. Out of all the sessions I attended, this was probably the largest. The majority of attendees of this session were NHibernate advocates. For me, when it comes to NHibernate the jury is still out, the following article describes one of the reasons why.

Some Background

Just to give you some background, I have been using NHibernate in-depth on a project for the last 9 months. I am no expert in the field but no novice either. Before using and ORM tool, I would write stored procedures in order to access crud functions of the database. Actually, “writing stored procedures” is overstretching, I would use codegen tools, run them over the database and plug my code into the resulting assembly.  This was cool for a number of reasons, firstly the output assembly would show any changes or conflicts in code at compile time, and secondly it defined an explicit contract with my data store. I’m big on scaffolding in code (I know its not 100% TDD) but it provides a great way to visualise your overall code architecture. 

When I started working on the project, I had very little exposure to ORM tools. I had read blog posts & articles but had never really cut my teeth on the technology. I would say that in the .net world, NHibernate is probably the no 1 ORM tool available. NHibernate is a port of the java implementation Hibernate which has been popular for many years. 

The thinking behind ORM tools is that they “become the database” and that you really don’t need to waste time writing SQL anymore. I have to say, the moment I started reading about ORM tools I felt a little uneasy. SQL is a language I am very comfortable with, more to the point it has been specifically developed in order to query data in a mathematical and consistent way. The idea of not touching SQL and focusing purely in code seemed a little frightening to me.  Trying to put my fears aside, I joined the project and jumped two feet into NHibernate.

The Learning Curve

Now during the last 9 months I can honestly say that the NHibernate learning curve can be a little erratic. Initially the model seems simple with a mapping file connecting your entities to the database. But as you continue, issues such as inheritance, polymorphism & lazy loading all start to impact your productivity.  Now let me make it clear, all those issues can be solved with NHibernate in an elegant way, however finding that way takes time. One of the biggest problems I faced was an issue with entities that did not require to be saved. See the model below.
 

In this model we have all objects connected in a parent-child fashion. In this case the selected class which does not have a related database table, controls access into our complex system.  This posed a bit of an issue for me as Nhibernate infers saving via a mapping file and in this case the selected class does not exist in the parent mapping file. Therefore, I felt I had run into an Object/Relational Impedance mismatch. Again, it was just my understanding of NHibernate, I soon found that I could solve this problem using NHibernate Components. So as you can tell, NHibernate is a large beast and it issues such as these that can really throw a spanner in your productivity. But they are not issues that cannot be resolved. 

NHibernate’s inherent Tight coupling

As my confidence with NHibernate has grown, I have felt that most issues can be resolved, all you need is the knowledge. However, there is one issue that I feel NHibernate creates that cannot be resolved. As I mentioned previously, I used to use stored procedures to interact with my database. Stored procedures in my mind are great as they enforce a contract between application code and the database.

Recently however, I hear people at conferences make snide remarks about them, like they are some form of pollution in the their code. I find this reaction strange, everywhere else in our code we spend time make sure there are adequate layers in order to reduce complexity and shield us from breaking changes.  So why is it that I hear people turning their nose up at sprocs? The answer lies in the history of sprocs, I think many of us have experienced sprocs that may be 100’s or 1000’s of lines of code. These procedures are in essence business logic, it’s this bad habit that has forced us to abandon them. A decision I feel is incorrect. 

What stored procedures really are are “service contracts”, regardless of bad practices implemented by devs or dba's in the past, they still serve a purpose in that they decouple the database schema from our application code. Its this important fact that I believe NHibernate misses and as a result leads to tight coupling between application code and the database schema. (Please note: I know NHibernate can use stored procs instead but I question the value NHibernate offers over codegen in the same situation).   

Lets take a simple mapping file for an address entity (shown below)

In this mapping file, we can see that properties on our entity map directly to database columns. This is the most common pattern used by developers working with NHibernate.  Now let’s jump forward.. say 6 months after our application has gone to production, I am a DBA and I need to restructure the Address table as new services are consuming that data or execution plans are bringing the server to a halt.  I want to rename columns and move the notes field into a separate table specifically designed for notes data. I can do this via simple script, however what I do not realise (being the dba) is that I have an application that is tighly coupled to the address table. I have an issue where changing the database could break any number of applications that depend on the data. I just don’t know!! More importantly, the  developers who wrote the application are no longer around (written by a supplier perhaps). I don’t even know about the dependency FULLSTOP as I cannot see these application server that may sit in disparate regions. What we have effectively done is create a long dependancy & support issue for our applications.

 

We could look to resolve this issue by implementing our mapping file so that it points to a view rather than the table, this however will only go so far, updates and deletes may need to span many tables which will be impossible to implement via view.  

So what do we do?

The answer is not clear, NHibernate & other ORM tools rely on mapping properties to database columns. This pattern results in tight coupling that makes applications suffer from a poor resistance to change.  We as developers cannot assume that the database is just for the application we are developing, data is a commodity and therefore commonly shared amongst multiple services, and the querying of that data may be good for one scenario but not the other.

Its seems ORM’s fall right into this hole, they offer us productivty but at a cost. As I said, the jury is still out for me as to whether the pro's out weight the cons but being aware is vitally important. 

I have a number of other issues I have comne across since using ORM's and I will be looking to blog about these in the future.  

Permalink |  Trackback

Comments (3)   Add Comment
Re: NHibernate and Tight Coupling    By Ayende Rahien on Sun, 17 Feb 2008 22:42:35 GMT
David,<br>You generally do _not_ go and change a production database willy nilly.<br>NHibernate actually provide a layer of abstraction on top of the DB, since you can modify the DB and the mapping and not touch the code.<br>To compare to your example, you would change DB and SP, I would change DB and mapping.<br>I strongly suggest reading Database Refacotring, Ambler talks about it quite a bit

Re: NHibernate and Tight Coupling    By David Hanson on Mon, 18 Feb 2008 09:29:02 GMT
Oren, <br><br>The point I making is that once an application goes into production application developers may not be around to update mapping files anymore. But large enterprises often have DBA's to manage their database systems. The core issue really being is who is in charge of the abstractions layer? <br><br>I would argue a DBAA should have control as they may have to support many other applications that consume the data. SOA often requires this.

Re: NHibernate and Tight Coupling    By John Rayner on Thu, 13 Nov 2008 22:54:02 GMT
Surely the principle of SOA is that only the service would touch the database directly? Any other applications would then consume the service ...


Your name:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 
Tweets Minimize
Twitter / LordHanson
  1. LordHanson: #southeastern have once again proved their rolling stock can reach new lows.

    Published Wed, 01 Feb 2012 08:09:05 +0000 by
  2. LordHanson: Checked in at The Cards http://t.co/LANfHukb

    Published Sun, 29 Jan 2012 11:04:51 +0000 by
  3. LordHanson: @BillGates Run for president Bill.

    Published Fri, 27 Jan 2012 08:41:35 +0000 by
  4. LordHanson: @swhelband Again!

    Published Fri, 27 Jan 2012 08:39:44 +0000 by
  5. LordHanson: The update for the Nokia Lumia recently has done wonders for battery life! Good job #Nokia #windowsphone

    Published Fri, 27 Jan 2012 08:36:27 +0000 by
  6. LordHanson: Checked in at Victoria Station http://t.co/L0BJ5smd

    Published Thu, 26 Jan 2012 08:35:41 +0000 by
  7. LordHanson: @pdl_uk VAT No: 924 5933 08 Shoulder again! Dang!

    Published Wed, 25 Jan 2012 21:47:45 +0000 by
  8. LordHanson: @tommyjmquinn I think that would be easier. Next Thursday ok?

    Published Wed, 25 Jan 2012 21:04:46 +0000 by
  9. LordHanson: @tommyjmquinn London bridge doable?

    Published Wed, 25 Jan 2012 20:32:34 +0000 by
  10. LordHanson: @tommyjmquinn so where's the meet?

    Published Wed, 25 Jan 2012 19:04:02 +0000 by
  11. LordHanson: @tommyjmquinn your choice mate. Somewhere easy to get to from Bankside. :-D

    Published Tue, 24 Jan 2012 22:01:20 +0000 by
  12. LordHanson: @tommyjmquinn so Darius, Justin and me confirmed. Thursday good for you? Waiting to hear from Sal.

    Published Tue, 24 Jan 2012 21:47:21 +0000 by
  13. LordHanson: @mark_mann which is illegal I thought!

    Published Tue, 24 Jan 2012 21:46:17 +0000 by
  14. LordHanson: Details on Windows Phone 8 confirms NT kernel http://t.co/5Qg1MILl

    Published Tue, 24 Jan 2012 21:34:11 +0000 by
  15. LordHanson: But next target for framework is #winrt. Need to see if my dependencies like DI, RX, ReactiveUi etc will work. Hmm

    Published Mon, 23 Jan 2012 08:33:16 +0000 by
  16. LordHanson: @pdl_uk hey Phil, how's marathon training going?

    Published Mon, 23 Jan 2012 08:31:37 +0000 by
  17. LordHanson: So I now have a framework for apps which targets .net, Silverlight and windows phone. Thankyou project linker!

    Published Mon, 23 Jan 2012 08:28:08 +0000 by
  18. LordHanson: For some reason dropped twitter for a month. Can't say I missed it really. Maybe I need to broaden my follow list!

    Published Mon, 23 Jan 2012 08:24:44 +0000 by
  19. LordHanson: Soo much hype over SIRI when it came out yet I never see anyone use it and don't know anybody who does either. #apple #sooverhyped

    Published Mon, 23 Jan 2012 08:23:18 +0000 by
  20. LordHanson: #southeastern customer satisfaction survey given to me today. This should be fun! Bit wait......no extremely poor option! Just very poor.

    Published Mon, 23 Jan 2012 08:20:09 +0000 by
Print  
Archive Minimize
Print  
Contact me Minimize
Print  
Microsoft Certs Minimize







Print  
Silverlight News Minimize
Silverlight - Google News
  1. Windows Phone 8 - Silverlight Apps Are Legacy - iProgrammer

    Published Fri, 03 Feb 2012 13:03:27 GMT by
  2. Super Bowl Streaming Fail - StreamingMedia.com

    Published Mon, 06 Feb 2012 05:59:33 GMT by
  3. Delphi Developers Rejoice at Silverlight, FireMonkey and VCL Coming Together ... - San Francisco Chronicle (press release)

    Published Tue, 31 Jan 2012 17:34:58 GMT by
  4. WP7 Upgrades and WP8 - Silverlight or C++ - iProgrammer

    Published Tue, 31 Jan 2012 17:21:58 GMT by
  5. Watch The 2012 Super Bowl Online - SportsGrid

    Published Sun, 05 Feb 2012 23:15:21 GMT by
  6. US viewers can watch Super Bowl on Mac, iOS - Macworld

    Published Sun, 05 Feb 2012 20:22:31 GMT by
  7. Hydra 4 Sharpens Its Teeth, Breathes New Fire - Dr. Dobb's

    Published Sun, 05 Feb 2012 17:25:01 GMT by
  8. Will Silverlight live or die? Microsoft won't say - InfoWorld

    Published Fri, 27 Jan 2012 11:00:46 GMT by
  9. Cablevision Flips Live TV To Laptops With Microsoft Silverlight - Multichannel News

    Published Fri, 27 Jan 2012 17:24:53 GMT by
  10. Do SharePoint & Silverlight Have a Future Together? - CMSWire

    Published Mon, 16 Jan 2012 17:29:27 GMT by
Print