NHibernate

NHibernate Training Course In Mechelen, Belgium – April 23/25

2 commentsWritten on February 12th, 2012 by
Categories: NHibernate

I'm organizing a public NHibernate Training Course from April 23rd through April 25th in Mechelen, Belgium. This time, I'm taking care of the whole thing myself instead of working together with a more established training company (except for renting a suitable training location). This enables me to offer the course at a lower price than the previous public course. All details can be found here. Feel free to spread the word! :)

(Ab)Using Conventions To Enforce Good Practices

15 commentsWritten on January 26th, 2012 by
Categories: NHibernate, Performance

I always tell people to explicitly specify the lengths of their string properties in their NHibernate mappings for performance reasons. If you don't specify them, the ADO.NET parameter lengths of those strings will always be set to the length of the actual string value that's been assigned to the parameter. This is a problem for SQL Server, because it can't cache those statements as efficiently as it would if the parameter lengths were always the same for a given statement. Simply put, if you don't specify the lengths, SQL Server's statement cache gets polluted with a bunch of statements that are often the same, but they're considered to be different simply because of the lengths of those string parameters. And this can really hurt the performance of your application.

Of course, not everyone remembers to set those lengths, so I thought it'd be great if I could force people to do this. With a little creative use of Fluent NHibernate's conventions, it's quite easy to enforce this:

public class StringsMustHaveLengthConvention: IPropertyConvention, IPropertyConventionAcceptance
{
    public void Apply(IPropertyInstance instance)
    {
        var msg = string.Format("The string property '{0}' of type '{1}' does not have a length value specified, " +
            "which is required for performance reasons. Add something like this to your mapping override:\r\n" + 
            "\tmapping.Map(e => e.{0}).Length(50); // with an appropriate length for this property",
            instance.Property.Name, instance.EntityType.Name);

        throw new MappingException(msg);
    }

    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Type == typeof(string)).Expect(x => x.Length == 0);
    }
}

With that convention in place, you won't even be able to run your code until you've specified the string lengths :)

Review Of My NHibernate Course

3 commentsWritten on July 6th, 2011 by
Categories: NHibernate

Sorry for the shameful promotion, but you would do it too ;) . Bart Deleye was kind enough to write a review of my NHibernate course which i'd like to share here:

Davy's NHibernate course is an excellent cocktail based on ingredients like expertise, high quality content, hands-on labs and best practices.

Day 1 starts with an introduction on ORM's in general after which we take a deep dive in the different configuration options (both using HBM files and Fluent NHibernate) from which you’ll acquire a thorough understanding of the mapping capabilities and the different solutions NHibernate provides for the impedance mismatch. Afterwards the theory will be applied in practice during the hands-on labs that are test-driven by NUnit code samples. This style of exercises not only clearly illustrates the usage of NHibernate but also provides you a nice toolkit to experiment further after the course. We finished the day with best practices on transaction/session management in different environments (web applications, services and desktop applications).

During Day 2 you will grasp the fundamental details of all query methods available in NHibernate with their pro's and con's. The course is based on NH 3 by the way, so you will learn everything on HQL, Criteria, QueryOver and LINQ. During the hands-on labs you will implement some nice challenging queries. Afterwards you will learn a lot of best and worst practices regarding performance with optimizations like Futures (query batching), statement batching, executable HQL, fetching strategies, projecting, and leaning on the session cache. The second day is finished with a session on the different concurrency strategies.

Day 3 covers a lot of advanced techniques that make your NHibernate skill set complete like Identifier and Inheritance strategies, Advanced Mappings, Second Level Cache, Stateless Sessions, … To finish the course, you’ll have to fix some red tests during the exercises.

I can only recommend this course to every new, novice and experienced NHibernate user. Only a few weeks later I notice a lot of improvement amongst my team members!

Bart and his team were the first people to attend the course almost 2 months ago, so i wasn't entirely sure yet of the format and the content at that time. This week i'm actually doing the course for the third time, and apart from some very minor changes and tweaks, it's pretty much the exact same as when i did it the first time, except that i'm not nearly as nervous now as i was when i did it the first time. Luckily for me, his team was fun to hang out with and there was plenty of joking around which definitely made it easy to just be comfortable instead of being a nervous wreck :)

Recent Additions To My NHibernate Examples

4 commentsWritten on May 8th, 2011 by
Categories: NHibernate

I've been pretty busy lately with the prep-work for my NHibernate course and my NHibernate examples, which the course leans on pretty heavily. Below is a list of examples that have been added in the past few weeks:

  • optimistic concurrency
    • optimistic-lock="all"
    • optimistic-lock="dirty"
    • version
  • pessimistic locking
  • performance
    • executable hql (updates/deletes)
    • extra lazy collection
    • futures
    • paging through collections
    • proxies
  • querying
    • aggregate functions (HQL/Criteria/LINQ/QueryOver)
    • eager fetching (HQL/Criteria/LINQ/QueryOver)
    • group by (HQL/Criteria/LINQ/QueryOver)
    • ordering (HQL/Criteria/LINQ/QueryOver)
    • paging (HQL/Criteria/LINQ/QueryOver)
    • polymorphism (HQL/Criteria/LINQ/QueryOver)
    • projections (HQL/Criteria/LINQ/QueryOver)
    • restrictions (HQL/Criteria/LINQ/QueryOver)
    • subqueries (HQL/Criteria/LINQ/QueryOver)
    • unique results (HQL/Criteria/LINQ/QueryOver)
    • native sql
    • stored procedures

And there's quite a bit more to come in the next 2 weeks or so...

The examples now also run on SQL Server with both Fluent NHibernate as well as normal mapping files. If you're interested, you can get them here.

NHibernate Examples

11 commentsWritten on April 10th, 2011 by
Categories: NHibernate

I published some NHibernate examples almost 4 years ago, and they still get downloaded pretty frequently. Unfortunately they still use NHibernate 1.2 and are somewhat limited as to what they demonstrate. My NHibernate course is rather example/exercise-heavy so i thought it would be a good time to replace those outdated examples with new ones and make them available to anyone who wants them. The examples are not complete yet, but will be updated frequently in the next couple of weeks. After that, i intend to keep them up to date to demonstrate new NHibernate features as they are introduced with new releases. Of course, you're more than welcome to contribute examples of your own.

The solution consists of a suite of automated tests that run on a SQLite database, which demonstrate some features of NHibernate. All tests currently run with the classic HBM mapping files and FluentNHibernate. Choosing which of the two is used can be done by selecting the correct build configuration (either HBMSQLITE or FLUENTSQLITE) before running the tests. Running the tests doesn't require any software to be installed or any configuration to be performed by you. Though you can obviously run the tests on different databases if you want to. The object model that is used is based on Northwind with a couple of changes. The object model can evolve into whatever it needs to be to showcase NHibernate features/usage. Some other models might also be introduced later on to show features that wouldn't fit well with the Northwind-based model.

These examples could be useful to people who are new to NHibernate, but could be interesting for experienced NHibernate users as well, if only as a playground to quickly experiment with some features that you're unsure of.

You can find the code here or you can always download the latest version here.