Book Image

NHibernate 2 Beginner's Guide

By : Aaron Cure
Book Image

NHibernate 2 Beginner's Guide

By: Aaron Cure

Overview of this book

<p>NHibernate is an open source object-relational mapper, or simply put, a way to retrieve data from your database into standard .NET objects. Quite often we spend hours designing the database, only to go back and re-design a mechanism to access that data and then optimize that mechanism. This book will save you time on your project, providing all the information along with concrete examples about the use and optimization of NHibernate.<br /><br />This book is an approachable, detailed introduction to the NHibernate object-relational mapper and how to integrate it with your .NET projects. If you're tired of writing stored procedures or maintaining inline SQL, this is the book for you.<br /><br />Connecting to a database to retrieve data is a major part of nearly every project, from websites to desktop applications to distributed applications. Using the techniques presented in this book, you can access data in your own database with little or no code.<br /><br />This book covers the use of NHibernate from a first glance at retrieving data and developing access layers to more advanced topics such as optimization and Security and Membership providers. It will show you how to connect to multiple databases and speed up your web applications using strong caching tools. We also discuss the use of third-party tools for code generation and other tricks to make your development smoother, quicker, and more effective.</p>
Table of Contents (19 chapters)
NHibernate 2
Credits
About the Author
About the Reviewers
Preface
Index

Preface

NHibernate is a popular, fast growing Object-Relational Mapper (ORM) with a helpful community of seasoned developers. It is used in thousands of commercial and open source projects.

Armed with a set of simple (and mostly free) tools and the knowledge you'll gain from this book, you can quickly and easily create an entire data-bound website, desktop application, windows or web service, or virtually any other .NET project you can conceive.

What this book covers

Chapter 1, First Look, discusses what an object-relational mapper is, what NHibernate is, and the features it provides us.

Chapter 2, Database Layout and Design, discusses how your database is constructed, how the data is related, and how to optimize it for the best performance using NHibernate.

Chapter 3, A Touch of Class, explains how creating classes to represent your data makes it easy for you to work with the data and allows you to branch from the design of the database, if need be.

Chapter 4, Data Cartography, deals with the actual interface to the database with NHibernate, either using XML mapping files, Fluent NHibernate, or no mapping at all. We also talk about lazy loading, using a fieldnames structure to help avoid typos, and generating the database from our mapping files or classes.

Chapter 5, The Session Procession, teaches you how to create NHibernate sessions, which use database sessions to retrieve and store data into the database.

Chapter 6, I'm a Logger, teaches you how to use the log4net logging framework for creating our own logs and tap into the information provided by NHibernate (including SQL statements) to monitor and troubleshoot our application.

Chapter 7, Configuration, explains how to configure our application so, we are ready to retrieve and store data into our database. Additional configuration options are discussed, as well as optional configuration properties for particular situations.

Chapter 8, Writing Queries, discusses using NHibernate to actually retrieve data, to include individual records and collections of records. We talk about filtering records and using the fieldnames structure we created earlier to speed up our development. We also talk about adding parameters to make paging and sorting work in data-bound controls.

Chapter 9, Binding Data, explains how to use the data methods we created earlier to build a web application that uses our data access layer, the ObjectDataSource, and other data controls to retrieve, display, and even insert/update database data.

Chapter 10, NET Security Providers, presents information about controlling access to and restricting the usage of data within a .NET web application. We discuss the use of custom membership and role providers with NHibernate to control access to information.

Chapter 11, It's a Generation Thing, discusses how to use code generation techniques such as CodeSmith, NHib-Gen, and MyGeneration to automatically generate our data access layer to get us up and running that much quicker.

Chapter 12, Odds and Ends, discusses some advanced topics such as the Burrow session management framework and the NHibernate SchemaExport tool.

What you need for this book

To successfully complete the examples in this book, you will need a copy of either Visual Studio 2008 or 2010. You can use any version as long as it includes the web application projects. This could be either a Visual Web Developer Express version or any full version such as Professional.

In addition to Visual Studio, you will also need a SQL database server. The examples are generated using SQL Server Express 2008 and SQL Server Management Studio (SSMS) Express.

You will also need to download the NHibernate binary files from sourceforge.net.

Who this book is for

This book is for new and seasoned developers of .NET web or desktop applications who want a better way to access database data. It is a basic introduction to NHibernate, with enough information to get a solid foundation in using NHibernate. Some advanced concepts are presented where appropriate to enhance functionality or in situations where they are commonly used.

Conventions

In this book, you will find several headings appearing frequently.

To give clear instructions on how to complete a procedure or task, we use:

Time for action – heading

  1. Action 1

  2. Action 2

  3. Action 3

Instructions often need some extra explanation so that they make sense, so they are followed with:

What just happened?

This heading explains the working of tasks or instructions that you have just completed.

You will also find some other learning aids in the book, including:

Pop quiz – heading

These are short multiple choice questions intended to help you test your own understanding.

Have a go hero – heading

These set practical challenges and give you ideas for experimenting with what you have learned.

You will also find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "This will give us a new DLL project called Ordering.Data inside a folder named Ordering, which contains a solution named Ordering."

A block of code is set as follows:

using System;
using System.Collections.Generic;
using System.Text;

namespace Ordering.Data
{
    class OrderHeader
    {
    }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

public class OrderHeader
{
  public OrderHeader() { }
}

Any command-line input or output is written as follows:

07:18:08.295 [10] INFO NHibernate.Cfg.Configuration - Mapping resource: Ordering.Data.Mapping.Address.hbm.xml

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: Right-click on the Ordering.Console application, and select Add | New Item.

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book on, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Tip

Downloading the example code for the book

Visit https://www.packtpub.com//sites/default/files/downloads/8907_Code.zip to directly download the example code.

The downloadable files contain instructions on how to use them.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.