Coming Soon: Screencast Series: Summer of NHibernate

My company makes extensive use of the NHibernate O/R Mapper technology in many of our solutions and we sometimes hire new developers that have good skills and experience but who have no direct exposure to NHibernate (yes, it is possible to be a good software developer even if you don’t know NHibernate smile_wink but anyone worth hiring in the first place can easily pick it up with minimal friction).  Often, our strategy for bringing people up to speed on this has been to rely on word-of-mouth and osmosis (often via pair-programming) to get the points across, but now we have a planned staffing ramp-up of a magnitude that will likely make that approach unwieldy.

To address this challenge, I am producing a series of successive screencast sessions that cover the complete use and implementation of NHibernate.  Called Summer of NHibernate, this series of screencasts is designed to take the newbie who knows nothing at all about NHibernate and teach them everything they need to know to start using NHibernate in a real-world application by the end of the hot summer months.  These screencasts will act as a set of reference materials that we can provide to new hires to help them get ramped up on the use of NHibernate in a short-order.

Since it seems to me that others in the community might find these to be of interest, I have decided to release them for general download to anyone who has always wanted to look into NHibernate but never found the topic at all approachable.

As I’m creating these screencasts first and foremost for internal consumption by our own staff, some of the content is intentionally not technology neutral (e.g., we use SourceGear VAULT for our SCC platform, CodeRush + RefactorPro! as enhancements to our VS IDE, etc.) and I have made no effort to make the content more ‘generic’ but any outside viewer should still be able to find the content useful despite this slight internal bent.

Think of this as an opportunity to take a multi-part crash course in NHibernate (from zero to productive) for free by downloading the screencasts as they are released and following along.  Each screencast will be accompanied by a new post on my blog that talks about what was covered in more detail.  Hopefully, rather than sweating outside in the summer heat developers can sit inside in the air conditioning and watch these screencasts this summer smile_teeth

Why bother releasing internal training content for everyone else who wants it?  Read on…

ALT.NET and the Chicken vs. the Egg Problem

Over the past 6-9 months or so I have watched closely the growing community interest in what has come (rightly or wrongly) to be called ALT.NET.  Meshing very well with a number of my long-held beliefs about .NET software development (and good software development in general), ALT.NET just doesn’t seem all that ‘ALTernative’ to me since I have long embraced any number of non-Microsoft-proscribed software development practices and tools in support of those practices.

That being said, there are of course a large number of .NET developers (perhaps even the large majority) who have no idea what all this ALT.NET stuff is about.  A infrequent but recurring topic on the ALT.NET forum is that of "how do we lower the barriers to entry for someone wanting to get their feet wet with ALT.NET?".   This is is echoed by ALT.NET newbies who say "where is the book I can read to become ALT.NET?"

The big problem here is that while ALT.NET is (or at least tries to be!) much more about what practices you follow when developing with your tools than the tools themselves, skill/expertise with at least some of the basic tools would seem to be a prerequisite for focusing on some of the ALT.NET values when developing software.  This is the classic chicken-and-egg problem where you cannot practice the principles without using the supporting tools and you don’t recognize that you actually need any of the tools until you try to follow some of the core principles and find the out-of-the-box tooling provided by Microsoft insufficient to support what you want to do.

Why NHibernate?

So why start with NHibernate?  The premise behind this coming series of screencasts is that data access is something that just about every application developer will have to deal with in just about every application.  As such, starting to learn a tool that enables more efficient and effective data access is likely to be immediately applicable to more developers than, say, an Inversion-of-Control container (though this statement is certainly debatable).

Further, while object-relational-mapping may be unfamiliar to many non-ALT.NET developers, its core concepts are relatively approachable and can be (relatively) easily incorporated into any software development project without requiring huge redesign of the entire system (as something like dependency-injection/inversion-of-control might require to be effective).

Lastly, rightly or wrongly NHibernate seems to have become the poster-child of the ALT.NET movement, constantly being raised as evidence of a powerful tool developed to support principles and practices that Microsoft is only just now barely starting to warm to.  This makes NHibernate one of the most visible software tool of the ALT.NET developer group (although I would stress here that it is entirely possible to develop applications without NHibernate and still follow a good number if not all of the good software engineering principles espoused by the ALT.NET community).

As such, I feel strongly that mastering NHibernate represents the low-hanging-fruit of the ALT.NET recommended toolset — its ubiquity (and value) perhaps only eclipsed by that of any one of the unit testing frameworks out there.  If we can get users to begin to use something like NHibernate, they will be much more able to approach other tools in the ALT.NET world that support the practices that ALT.NET considers important to the development of professional software systems.


Following is my planned outline for each of the screencasts that will make up the series…


  • Get the NHibernate dlls and .xsd files
  • Demonstrate how to add the xsd files to VS XML intellisense capabilities to get syntax support in the XML files
  • Use a simple Customer — Orders — OrderItems style database
  • Hand-code DTO class for the ‘Customer’ table
    • Introduce the concepts of 1:1 relation between table and class
    • (the dumb-DTO pattern; start here since it’s by far the simplest way to work)
  • Hand-code the Customer.hbm.xml mapping file to expose people to this syntax
  • Hand-code the hibernate.cfg.xml file to expose people to this syntax
  • Setup the project in VS (data access class lib)
  • Setup a corresponding test class to go along with it (to be used to exercise the data access class lib)
    • Further demonstrates the value of unit tests as a ‘harness’ for exercising concepts in a safe vacuum
  • Code a simple method that demonstrates the basic mechanics of interacting with NHibernate to retrieve all Customers from the database
    • ISessionFactory, ISession, reading configuration(s)
    • Leveraging the using ( ) {…} block to ensure session is properly closes/disposed of when done



  • Extend the data access classlib from session 1 to include additional query methods to return Customers from the database using all the different query approaches supported by NHibernate
    • Demonstrate the Criteria API syntax to retrieve using the NHibernate.Expression.* statements
    • Demonstrate QBE
      • Code retrieval method that takes a customer and returns all matches to that example object
    • Demonstrate HQL queries
      • String-literals
      • Polymorphic queries
    • Demonstrate Projections in queries
      • GroupBy
      • OrderBy
      • Max
      • Avg
      • Etc.



  • Continue with query option examples if incomplete from session 2 (as needed)
  • Explore INSERT, UPDATE, DELETE semantics in NHibernate
    • Extend data access classlib to include data methods to allow updates/inserts/deletes
    • Introduce concepts around session.Flush(), session.SaveOrUpdate()
    • Introduce the ITransaction pattern to wrap CRUD operations



  • Extend the collection of DTO classes to include the other tables
  • Extend the mapping files to support the new DTO classes
  • Exploring multiple-class mappings and FK relations in the mapping files
    • Relationship types
      • One-to-Many
      • Many-to-One
      • Many-to-Many
    • Collection types
      • Bag
      • List
      • Set
      • Etc.
    • Understanding cascade-xxx operations (and its impact on orphans)
  • Extend the data access classlib to implement these things by way of examples
  • Introduce the concepts of Lazy-loading



  • Querying complex object graphs (parent à children, collections, etc.)
    • Querying across object relationships
      • Joins
      • Sub-queries
      • Child criteria
      • Others
    • Via the Criteria API
    • Via HQL
  • Understanding NHibernate support of the VIEW construct
    • Mapping files for VIEWs
    • Classes for VIEWs
  • Understanding disconnected Objects and session.Evict( )
    • Multithreading issues/concepts


The first screencast installment will be ready by about June 20, 2008~!