Posts Tagged ‘nhibernate’

Programmers – Time to Make Up Your Own Mind

2011/10/02 2 comments


In this day and age of being told what we should think, whether if we should like a film or not; a restaurant or not;  a make of car or not;  or what type of food we should minimise; so to is the almost equivalent to religious evangelism publicised by certain authors, speakers or even bloggers (yes I am a blogger too) on their idea on how we should program,  or SDLC patterns such as source code control or how to implement say continuous integration and/or deployment.   Many of the suggestions are brilliant, helpful and well deserved, particularly for those with little experience in the field.   However it is all too easy to become a disciple of the new religion without questioning what is being said.

As with most things,  a teacher with incorrect information or a teacher with an agenda is a bad teacher with a motive.  The same is said for programming authors or speakers.  Perhaps they want to sell you more books?  Have YOU thought about it?

I mentioned that many will accept an idea without questioning the mechanics behind it – take database object relational mapping (ORM) made arguably famous by a certain well known author and speaker.  People like it cause it makes the DB look like an object-orientated tree graph.  Nothing wrong with that, I particularly like canonical protocols or singular agnostic APIs.   If I were to take the critical approach to ORM I could encounter:

“awww everyone else is doing it;  It’s much easier!”  – they say

Is a common defence.   Fair enough, but it demonstrates the obvious and unfortunate scenario that certain developers will do a thing if there is sufficient evidence for doing so in the first place (particularly if it requires zero-coding on their part) or if it makes their job easier.  The alternative is not to use ORM but to use more complex and time consuming patterns such as say stored procedures.  I’m not saying stored procs are better as that would be hypocritical to this topic.  I’ll let you decide.  I find some developers are not aware or do not want to think about and or investigate for themselves the overhead of data transformation let alone network activity.

Which is faster –

  • the system where the majority of data processing happens inside the database in stored procs with minimal data transformation or
  • the system which the majority of data processing occurs external to the database,  subject to data transformation via an ORM data typically on another computer via a network?

I’ll let you decide that.

It doesn’t have to be an ORM either.  Perhaps they have not fully thought about log4net, StructureMap, ASP models.  Are they good tools?  What are the pros?  Are there any cons?  A project for the reader.

“Ease does not necessarily imply high performance in arguably the same way as complexity does not imply unreliability.” – Me

Many say simplicity is best.  Perhaps.    The question is have YOU thought about it?  What does it mean to you?  A project for the reader.

Which is better – a turn of the century zepplin or a contempory Boeing 747 Jumbo Jet?  Which is more reliable?  Which has more fail-safes?  A project for the reader.

Don’t let your instincts prejudice your eventual decision making process.  Think before you say something I always say.  Question and debate those in authority, but try not to disagree if the only evidence on your behalf is that others are doing it your way.  You have not contributed anything personal to the debate.  Again think for yourself and come up with your own evidence.  This is what I encourage of my juniors.

“Extraordinary claims requires extraordinary evidence”

Some programmers sadly are so keen to use well known off the shelf systems with little thought as to their actions.


A need for inversion of control (IoC) such that a database or logging object can be wired to client objects is quite understandable.  Such things are quite nice and do seem to make our jobs easier.  IoC systems typically require configuration as I have blogged before.  You must ask yourself –

  • is the configuration difficult?
  • Is the configuration error prone?
  • Is the configuration open to security vulnerabilities?
  • Does it make it all worthwhile?
  • Have you considered a plain 1990s Class Factory Pattern?

A project for the reader.  Again ease may not necessarily mean correctness.  What do you think?

Don’t think something just because someone told you to – learn to think for yourself again.

Again, this is not specific purely in the IT field but can apply to our daily lives too.

WCF and nHibernate Can Live Together in Peace

2011/04/17 Leave a comment

WCF and nHibernate are two rather useful technologies for creating say SOA Entity Services[1].  WCF, particularly in .NET 4 makes it extremely easy for authoring a communications conduit whilst nHibernate unquestionably does to DB access as WCF does to communications.  Whether or not nHibernate has a place in large scale enterprise is a topic for another day.

I recently came across some peer code which cleverly defined a custom WCF service behaviour for initialising nHiberate when the service is first accessed and then for each method call.  It made good use of  the ability to utilise .NET attributes applied to the service class so developers in effect merely had to add one line of code to turn a WCF service into a DB-aware one. Quite well done I thought.

A problem occurred though when one wanted to just see if the service was published correctly by browsing to the .svc file in a web browser.  Instead of the usual WCF greeting displaying pseudo client code in a web page, an error and stack dump was displayed mentioning something about nHibernate.

Unfortunately, the mere presence of the class-level attribute meant that the nHibernate custom service behaviour was being called and I had not setup my database or some such.  Interestingly the error occurs even though we were not invoking any methods.  This meant it was technically difficult to verify the successful publication or hosting of the WCF service.  More disturbing was that it now broke all my MS Test tests for instantiating and invoking my WCF service.  Needless to say TeamCity was now displaying lots of red lights.

We discussed this and came up with an idea of moving the nhibernate service behaviour into the service configuration (in this case the web.config file) rather than hard code it explicitly with attributes or inline code.  I’m a great fan of defining behaviour by configuration irrespective of what technology the config is persisted as.  The beauty of this approach is that we can take advantage of web.config transforms for each build configuration.  Configurations for build, release, with DB and without.  Just perfect for continuous integration testing of the service itself but also deployed testing between client and service.

WCF allows you to define your own custom service behaviours and register them in the config file. This can be done manually by web.config heroes or via the magnificent WCF Service Configuration Editor.

First you need to define an Element Extension. Basically this represents the XML node in the config file. It acts as a moniker for creating an instance of your extension.  It is this moniker that the Service Config editor detects and not the behaviour extension itself.

public class NHibernateServiceBehaviourExtensionElement : BehaviorExtensionElement


protected override object CreateBehavior()


return new MyNHibernateServiceBehaviour();


public override Type BehaviorType


get { return typeof(MyNHibernateServiceBehaviour); }



Then to define the actual extension:

public class MyNHibernateServiceBehaviour : Attribute, IServiceBehavior { … }

Put both of these into ideally the same assembly.  Because of the way WCF and the Service Editor works the assembly will need to be in the same path.  This is arguably not feasible for all occasions so we elected to place them in the GAC.  Luckily nhibernate, log4net, hibernatinglamas (or was that rhinos) are all strong named.

With that done, I recommend that you use the WCF Service Config tool to do the next bit.  After all, if this part fails then it will most likely will in production.


Save that to your typical config transforms and then it keeps everyone happy, even grumpy slappers of nHibernate such as me. 😉  This approach will allow you to have full debug WCF-only configs for local testing; full debug WCF+DB testing;  CI WCF only and CI WCF+DB.  Configs for all occasions.

Happy happy joy joy!

[1] Thomas Erl, Patterns of SOA Design

Categories: Development Tags: , ,

The Emperor’s New Clothes – Affects Developers Too

2010/03/30 Leave a comment

…Two weavers who promise an Emperor a new suit of clothes invisible to those unfit for their positions or incompetent. When the Emperor parades before his subjects in his new clothes, [only] a child cries out, "But he isn’t wearing anything at all!".  [Everyone else was was too scared to say anything.] [3]


Something that I have noticed about the software development industry is that people will go to extraordinary lengths to author the most well structured software system.  This includes the utilisation of wonderful design patterns from the original Gang of Four in addition to new contemporary additions; the surprising new stage of developers not only adding test cases but also creating them themselves in the form of NUnit or MSTest and adding them to their project; the use of object-relational mapping (ORM) technologies and more recently, the use of something supposedly new called inversion of control [+ dependency injection] (IoC DI)[1].

The consequence of all of this leads to not just an improved working experience for the developer, but also creates software that is better structured, designed, loosely-coupled and obviously testable.  Developers have been wonderful in their approach to ‘let’s stop for a minute and do this better’ mentality and history has shown that all these new tactics have been quite successful leading to well-rounded reliable software.

However, in order for many of the above mentioned technologies/patterns/practises to work, generally requires developers to resort to doing something quite disturbing, something that requires complicated procedures; something that is error prone; something that can be a security risk; and in some cases something that can lead to application instability.

I speak of course is the practice of developers hand-editing XML files for the purpose of configuring a technology or 3rd party system that is either a framework or library.  XML when used right can be rather wonderful, I fully support it for scenarios such as data interoperability, transformations, SOAP messaging and so on.  I have no problem with a configuration tool or system saving application settings to XML, just so long the user experience is not hand-edited-XML-first.  Contrary to widespread belief, XML is not human readable, I doubt your English-speaking grandmother will be able to decipher it.  Nor is XML self-correcting – just try deleting a ‘>’ and see what happens.  This is why it should never be used for manual editing regardless of skill, I doubt anyone can claim 100% hand-editing without errors.

Libraries such as NHibernate require configuration before it can be used – fair enough, but to force developers to manually tweak XML configuration files is not only time consuming it is also error prone.  I believe there are now tools to generate the XML files.

IoC systems such as StructureMap also require configuration. Most uses I have seen is via hand-edited XML (though I believe runtime calls are also available). The issue here is that configuration for SM is subject to be:

  • error-prone
  • type-safety concerns
  • tedious
  • can lead to application instability[2]
  • can lead to security issues[2]

Then there are the technologies that take XML to heart literally, the authors being fan boys of XML so much they include it in the name of their system. The bizarre installer technology Wix and Microsoft’s XAML are two examples.   Wix offers developers questionable value whilst at the same time opens up a whole new set of problems for installer developers:

  • error-prone
  • lack of a rich developer experience
  • tedious
  • significant increase in time investment for making an installer

Shame on you Visual Studio users, have you not seen the Setup project wizards?  Click a few buttons and away you go. You can even do custom build actions in type-safe .NET should the need arise!

Don’t get me started on XAML. Whoever thought programming in a data-structure file format was a good idea has issues. Hand-edited XSLT is confusing enough.  I get the impression that Microsoft wanted to release an alternative to Windows Forms development but instead of providing a proper editor they just deployed XAML and said “…there you go! have fun!”. we certainly got fooled into thinking hand-editing XAML is pretty neat.

Personally I feel Wix and XAML share many traits, they both represent a backward step for the developer experience; crazy programming in a data-structure mentality; and also highlight a lack of a good IDE tool.

On the subject of Microsoft, their original sin was to allow or to give the impression to developers that the creation, deployment and hand-editing of xxx.exe.config files is the norm.  This practice remains to this day even surprisingly after Vista Development Guidelines stipulate that no files should be written to in .\Program Files by the application after the application has been installed (ignoring patches).  So you shouldn’t be updating xxx.exe.config files there.  Yeah I know there is the file virtualisation thing, but then again I can call WriteProfileString to write to the Win.ini file.  It does not mean that because you can do something, that you should do something.

Microsoft followed this up by allowing .NET Remoting and later WCF configuration to be, by default, persisted to the application’s xxx.exe.config file.  Again it’s bad form (see my prior post) because a user can easily change application settings that can have huge ramifications on your application. Case in point – WCF allows you to specify a transport provider as well as transport attributes. A user can change your finely-tuned, well tested and approved TCP/IP binary, no encryption, SOAP message settings to say https and now suddenly your application’s message size has grown considerably.  Not only that, because the user indicated either consciously or though accident WS-R, it’s no longer a one way message but perhaps up to four or so!

The innocent editing of a plain-view configuration file can have major ramifications for not just application behaviour but also application performance!  A GUI Configuration tool provides a rich user experience – it warns the user; it usually has some form of online help.  A XML config file does not!

Some of the technologies I mentioned have tried to clean their act up, unfortunately, the damage has been done perhaps regardless of the good intentions of follow-up tools in reducing the XML hand-editing.    However, people continue to hand-edit XML even with the knowledge of richer API counter-parts or the use of additional helper tools.

So I think or perhaps hope that developers are aware that providing complex configuration via hand-edited XML or to program in XML-related data structures is not really ideal, it’s a bit like the Hans Christian Andersen story – The Emperor’s New Clothes.


Most of us know its not really a good idea but we don’t do anything about it.

Until next time…

[1] IoC DP is nothing new, anyone familiar with COM Shell Namespace Extensions plug-ins development (that has pretty much not changed since Windows 95) will see similarities. Microsoft Management Console snap-ins is another example. I’m quite sure avid readers will find other examples that predate my computer lifetime.  IoC incidentally merely means the API calls into your code, a button click event is a good example.

[2] because types and methods must generally be exposed publically for SM IoCDP to work, together that most configuration is via a XML config file, a reasonably skilled malicious user can alter your config to call a different type or member thus changing the behaviour of your system in an unforseen way. e.g. You might have accidently exposed a type or method publically thus allowing the malicious user to use that type instead. This type may offer similar functionality but could lead to a drastic outcome.  Perhaps this type was for testing purposes only.

[3] wikipedia –