NHibernate 2 Beginner’s Guide – Book Review


Book on Amazon

Finally someone has written an in depth beginners book for nhibernate.  Nhibernate 2.0 Beginners Guide written by Aaron Cure is just that and more.  Wow do I sound like a car salesman.  I was a little disappointed when I read Nhibernate In Action last year, because it was more of a reference than a tutorial.  This book is definitely what the title states, which is a beginners guide.  The book contains step by step examples of how to find, setup, and use nhibernate.  I highly recommend this book to anyone wanting to learn nhibernate.  By the way I think all .netters should do just that.

Pros

Cons

Chapter Notes

  • Share/Bookmark

NHibernate in Action – Book Review


Book on Amazon

All summer I meant to read the book “NHibernate In Action”. I finally got around to it, better late than never I guess. In my opinion this book is aimed at higher level programmers such as lead developers or architects. The book does not contain clear step by step examples of how to get NHibernate running. However it does provide in depth conceptual reasoning why anyone would want to use NHibernate along with reference style examples. If you are trying to learn NHibernate  you are better off reading a few online tutorials first and after that read this book so you can understand exactly what NHibernate is doing and how it does it. Below are some book pros & cons, NHibernate links, and chapter summaries.

Pros

Cons

Helpful NHibernate Links

Chapter 1

Chapter 2

Chapter 3

Chapter 4

Chapter 5

Chapter 6


  • Share/Bookmark

Configuring NHibernate in a Multiple Project Layout

I recently had to setup NHibernate in a multiple project c#, asp.net MVC solution. Since most of the tutorials and books only show a basic single project layout I thought I would post an example of how to configure NHiberate with a multi-project solution.

The main problem I had was that my model objects or entity objects were in a separate project than my database access code which contained the .hbm.xml mapping files. When I tried to configure NHibernate with the basic examples I was getting an “NHibernate.MappingException : No persister for: YourClass” exception. I was still getting that exception even after changing the .hbm.xml files to be an “Embedded Resource”. I finally figured out that my .hbm.xml files needed to reference the ModelLayer assebmly and my NHibernate.Cfg.Configuration.AddAssembly() needed to reference the DataAccessLayer assebmly. See screen shots and code examples below for more details.

Solution layout

  • DataAccessLayer
    • Duh. Database code
      • References
        • ModelLayer
  • ModelLayer
    • Model objects
  • MvcAgain (web layer)
    • ASP.Net project
    • References
      • ModelLayer
      • ServiceLayer
  • ServiceLayer
    • Business logic
    • Reusable logic
    • References
      • ModelLayer
      • DataAccessLayer

Model objects Car.cs and CarLot.cs

using System.ComponentModel.DataAnnotations;
namespace MvcAgain.Models {
    public class Car {
        [Required]
        public virtual int Id {get; set;}
        [StringLength(35), Required]
        public virtual string Manufacturer {get; set;}
        [StringLength(35), Required]
        public virtual string Model {get; set;}
        public virtual CarLot Lot {get; set;}
    }
}//end of Car.cs

using Iesi.Collections;
using System.ComponentModel.DataAnnotations;
namespace MvcAgain.Models {
    public class CarLot {
        [Required]
        public virtual int Id { get; protected set; }
        [StringLength(35), Required]
        public virtual string Name { get; protected set; }
        public virtual ISet Cars { get; protected set; }
    }
}//end of CarLot.cs


Car.hbm.xml and CarLot.hbm.xml

These .hbm.xml files are in the DataAccessLayer project, but they reference the ModelLayer assembly.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="ModelLayer"
                   namespace="MvcAgain.Models">
  <class name="MvcAgain.Models.Car" lazy="false" table="cars">
    <id name="Id">
      <generator class="native"/>
    </id>
    <property name="Model"/>
    <property name="Manufacturer"/>
    <many-to-one name="Lot" class="CarLot" column="lotid" not-null="true">
    </many-to-one>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="ModelLayer"
                   namespace="MvcAgain.Models">
  <class name="MvcAgain.Models.CarLot" lazy="false" table="carlots">
    <id name="Id">
      <generator class="native"/>
    </id>
    <property name="Name"/>
    <set name="Cars" inverse="true">
        <key column="lotid"/>
        <one-to-many class="Car"/>
    </set>
  </class>
</hibernate-mapping>


NHibernateSessionFactory.cs (NHibernate configuration code)

using NHibernate.Cfg;
using NHibernate;
namespace MvcAgain.DataAccessLayer {

    public class NHibernateSessionFactory {

        private static ISessionFactory sessionFactory = null;

        private static ISessionFactory SessionFactory {
            get {
                if(sessionFactory == null) {
                    Configuration configuration = new Configuration();
                    configuration.Configure();
                    //note: you must use the name of the assembly
                    //that contains the .hbm.xml mapping files.
                    //For this example NHibernate will load all .hbm.xml files it
                    //finds in the DataAccessLayer assembly
                    configuration.AddAssembly("DataAccessLayer");
                    sessionFactory = configuration.BuildSessionFactory();
                }
                return sessionFactory;
            }
        }

        public static ISession OpenSession() {
            return SessionFactory.OpenSession();
        }
    }//end of class
}//end of namespace


ModelLayer assembly name in the properties pane.



  • Share/Bookmark