NHibernate 3 Beginner's Guide

More Information
  • Create a domain model
  • Learn different techniques to map the domain model to the underlying database
  • Build the database from your model automatically
  • Use session and transactions to persist and retrieve data
  • Efficiently query the database by using LINQ to NHibernate and HQL
  • Configure your system for use of NHibernate in a clear and concise way
  • Write unit and integration tests for your data access layer
  • Learn about the common pitfalls that should be avoided
  • Implement data validation on the entity level
  • Monitor and profile your data access

Ideally, we would want to persist the objects our application uses and produces AS IS, without having to first transform them in complex ways. NHibernate is a framework that provides us with an object-oriented access to a relational database without having to write SQL and with little or no database-specific data access code. Definitely, if you are a .NET developer, knowing more about NHibernate will simplify and reduce your efforts in developing .Net applications.NHibernate 3 Beginner's Guide introduces Nhibernate with step-by-step examples, and is the easiest way to learn about bridging the gap between object-oriented .NET applications and the relational database that stores the application's data. It is a beginner's guide to NHibernate that starts from scratch. Successive chapters build upon earlier concepts, while the sample code presents various ways to accomplish typical data access tasks.

NHibernate 3 Beginner's Guide examines all of the topics required to get a functional data access layer implemented while writing the least amount of code possible, presenting options along the way to handle particular edge cases or situations as they arise. The book begins with an introduction to NHIbernate 3 and then moves on to creating the development environment. Then it teaches you how to create a model, define a database schema, and map the model to the database then covers sessions and transactions. This is succeeded by testing, profiling, and configuration, validation of data and writing queries. Finally, we wrap up with notes on the common pitfalls that you should avoid.

Once you complete reading the book, you will have gained the skills and knowledge to incorporate NHibernate into your own applications.

  • Incorporate robust, efficient data access into your .Net projects
  • Reduce hours of application development time and get better application architecture and performance
  • Create your domain model first and then derive the database structure automatically from the model
  • Test, profile, and monitor data access to tune the performance and make your applications fly
  • Clear, precise step-by-step directions to get you up and running quickly
Page Count 368
Course Length 11 hours 2 minutes
ISBN 9781849516020
Date Of Publication 24 Aug 2011
Prepare our development environment
Defining a model
Time for action – Creating the product inventory model
Mapping our model
Time for action – Creating the mapping classes
Creating the database schema
Time for action – Creating the database
Creating a session factory
Time for action – Creating a session factory
Opening a session
Time for action – Opening a session to the database
Persisting objects to the database
Time for action – Adding a new category to the database
Reading from the database
Time for action – Loading the list of all categories from the database
Doing the same without NHibernate – using ADO.NET only
What is a model
Model first versus data first
Elements of a model
Time for action – Creating a Name value object
Creating an entity
Time for action – Creating a base entity
Time for action – Creating a Customer entity
Defining relations between entities
The order entry model
Time for action – Implementing an order entry model
What is a database schema?
Time for action – Creating the OrderingSystem database
Laying the foundation – table layouts
Time for action – Creating the Categories table
Time for action – Defining a script to create the Products table
Table columns
Relations, constraints, and indices
Time for action – Adding a constraint to the Product table
Time for action – Creating a script to add a check constraint
Time for action – Adding an index using the designer
Time for action – Creating a script to add an index
Normal form
Putting it all together
Time for action – Creating a schema for the order entry system
Do not use database-generated IDs
What about stored procedures and triggers?
What is mapping?
Types of mapping
A word about lazy loading
Creating database schema creation scripts
Fluent mapping
Time for action – Mapping our domain
Use mapping conventions
No mapping; is that possible?
Time for action – Using auto-mapping
Time for action – Using ConfORM to map our domain
XML mapping
Time for action – Mapping a simple domain using XML
Why do we need tests?
What should we test?
What about the database?
Preparing our environment for testing
Time for action – Creating the base for testing
Time for action – Using SQLite in our tests
Time for action – Adding logging to our application
Time for action – Enable logging in NHibernate
Monitoring and profiling
Time for action – Adding NHibernate Profiler support
Why do we need a configuration?
Elements of the configuration
XML configuration
Time for action – Configuring NHibernate using XML
Configuring NHibernate in code
Time for action – Configuring NHibernate in code
Fluent configuration
Time for action – Using Loquacious to configure NHibernate
Convention over configuration


Aaron Cure

Aaron Cure is an avid developer, instructor, and innovator. During his 10 years in the military as a linguist and a satellite communications repair technician, he learned that his real love was computer programming. After various throes with PHP, Classic ASP, VB, and a brief encounter with Java/JSP, he found a real passion for the .NET framework. After searching for a "better way" to carry out database storage and retrieval, Aaron stumbled across the NHibernate framework. Unsatisfied with the options for interacting with this great framework, he founded the NHibernate Generation project (nhib-gen) on SourceForge to reduce the "barrier to entry" for most developers. Aaron and his family run a small consulting and web hosting company doing web design and custom software development for various organizations across the country. One of their more interesting projects has been software to control laser cutting machines. In his spare time, he also enjoys developing projects with his daughters, using everything from Lego NXT (using C# and Bluetooth communications) to the Microchip PIC platform (using JAL and USB). He also collects and restores classic farm tractors, engines, and farm equipment, as well as semi trucks and trailers. He and his family display them at tractor shows, parades, schools, and various other community events.

Dr. Gabriel Nicolas Schenker

Gabriel N. Schenker started his career as a physicist. Following his passion and interest in stars and the universe he chose to write his Ph.D. thesis in astrophysics. Soon after this he dedicated all his time to his second passion, writing and architecting software. Gabriel has since been working for over 12 years as an independent consultant, trainer, and mentor mainly on the .NET platform. He is currently working as chief software architect in a mid-size US company based in Austin TX providing software and services to the pharmaceutical industry as well as to many well-known hospitals and universities throughout the US and in many other countries around the world. Gabriel is passionate about software development and tries to make the life of developers easier by providing guidelines and frameworks to reduce friction in the software development process. He has used NHibernate in many different commercial projects, web-based as well as Windows-based solutions. Gabriel has written many articles and blog posts about different aspects of NHibernate. He is the author behind the well-known NHibernate FAQ blog. Gabriel has also been a reviewer for the bestselling NHibernate 3 Cookbook. Gabriel is married and father of four children and during his spare time likes hiking in the mountains, cooking and reading.