Friday, September 10, 2010
 
   
 
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: Experienced .net dev in sydney for next 5 months if anyone needs me. CV on request just tweet me.

    Published Sun, 29 Aug 2010 05:41:05 +0000 by
  2. LordHanson: Flash on iPad....nice. http://www.tipb.com/2010/07/04/frash-android-flash-ported-ipad/

    Published Sun, 04 Jul 2010 22:07:55 +0000 by
  3. LordHanson: Anyone noticed that when typing on your iPhone it sounds like your holding a gieger counter?

    Published Sun, 04 Jul 2010 22:05:28 +0000 by
  4. LordHanson: Missing wacko's music... What's happened to the album he was working on before he died?

    Published Fri, 25 Jun 2010 23:01:45 +0000 by
  5. LordHanson: New version of Connectify cannot recognise my active Internet connection! Had to roll back to previous version! #fail

    Published Fri, 25 Jun 2010 22:54:54 +0000 by
  6. LordHanson: vuvuzela blowing spoils the world cup! Fact!

    Published Mon, 14 Jun 2010 05:08:43 +0000 by
  7. LordHanson: About http://www.theaustralian.com.au/business/news/us-competition-regulators-to-investigate-apple/story-e6frg90x-1225878779986

    Published Mon, 14 Jun 2010 00:04:45 +0000 by
  8. LordHanson: In the camper van and a storm is coming.....How exciting.

    Published Sun, 25 Apr 2010 04:39:48 +0000 by
  9. LordHanson: My vaio p is doing well while travelling. 3g Internet, HD movies, digital tv, photo editing, wifi router for iPods and much more. Love it

    Published Wed, 10 Mar 2010 10:29:19 +0000 by
  10. LordHanson: Ok so I need to stay techie while away from a computer for a year. Anyone got any ideas.

    Published Mon, 22 Feb 2010 12:31:09 +0000 by
  11. LordHanson: Sitting in YHA Glebe Sydney waiting for the movie night to start

    Published Thu, 18 Feb 2010 08:13:10 +0000 by
  12. LordHanson: Madness today. We only booked our return tickets to bangkok on the wrong day! Luckily we managed to change them!

    Published Wed, 10 Feb 2010 15:54:37 +0000 by
  13. LordHanson: HTML5 the future? http://bit.ly/6yf9Bu

    Published Tue, 09 Feb 2010 13:43:48 +0000 by
  14. LordHanson: Last night in Bangkok! Good fun!

    Published Thu, 04 Feb 2010 18:09:38 +0000 by
  15. LordHanson: @trampussandal Dad? lol

    Published Thu, 04 Feb 2010 05:26:39 +0000 by
  16. LordHanson: Im sitting in a coffee shop in my home town of epsom thinking... Man the day has finally arrived. I can feel the stress lifting.

    Published Mon, 01 Feb 2010 09:05:13 +0000 by
  17. LordHanson: So what excuse will apple use to not allow flash or silverlight to run on the ipad this time I wonder.

    Published Fri, 29 Jan 2010 19:07:46 +0000 by
  18. LordHanson: Yay just manage to upgrade from vista ultimate to windows 7 enterprise by using the registry hack trick. No reinstalls.

    Published Wed, 27 Jan 2010 07:18:07 +0000 by
  19. LordHanson: @swhelband Sure am...http://bit.ly/aZ6Xvd

    Published Tue, 26 Jan 2010 19:05:18 +0000 by
  20. LordHanson: I finished work today in prep for travelling. I must admit as i left the office i felt a little emotional. Sign of a good job with great ...

    Published Tue, 26 Jan 2010 17:48:49 +0000 by
Print  
Archive Minimize
Print  
Contact me Minimize
Print  
Microsoft Certs Minimize







Print  
Silverlight News Minimize
Silverlight - Google News
Print