PacktLib: NHibernate 3 Beginner's Guide

NHibernate 3 Beginner's Guide

Credits

About the Authors

About the Reviewers

www.PacktPub.com

Preface

First Look

What is NHibernate

What is new in NHibernate 3.0

Why would I use it

Where do I get it

Is there documentation available

Can I get help using NHibernate

Is there commercial support available

Who uses it

Summary

A First Complete Sample

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

Summary

Creating a Model

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

Summary

Defining the Database Schema

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

Views

What about stored procedures and triggers?

Summary

Mapping the Model to the Database

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

Summary

Sessions and Transactions

What are sessions and transactions

The session factory

Creating your first session

First level cache or identity map

No database operation without a transaction

NHibernate session versus database session

Time for action – Creating a session and doing some CRUD

Session management

Time for action –Implementing session management for a web application

Unit of Work

Handling exception

Second level cache

Time for action – Using a second level cache

Summary

Testing, Profiling, Monitoring, and Logging

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

Logging

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

Summary

Configuration

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

Summary

Writing Queries

How can we get to our data?

The LINQ to NHibernate provider

Creating a report using LINQ to NHibernate

Time for action – Preparing the system

Time for action – Creating the reports

Criteria queries

Time for action – Using QueryOver to retrieve data

Hibernate Query Language

Lazy loading properties

Executing multiple queries in a batch

Eager loading versus lazy loading

Bulk data changes

Summary

Validating the Data to Persist

What is validation and why is it so important?

Who owns the database?

Why, what, and where do we validate?

Validating single properties

Time for action – Using property validation

Validating complex business rules

Time for action – Validating user input

What to do if we don't own the database?

Summary

Common Pitfalls—Things to Avoid

Requesting the obvious

Wrong mapping for read-only access

Blindly relying on NHibernate

Using implicit transactions

Using database-generated IDs

Using LINQ to NHibernate the wrong way

The trouble with lazy loading

Using one model for read and write operations

Phantom updates

Time for action – Causing a phantom update

Using NHibernate to persist any type of data

Summary

Pop Quiz Answers

Pop Quiz Answers

Pop Quiz Answers

Pop Quiz Answers

Pop Quiz Answers

Pop Quiz Answers

Pop Quiz Answers

Pop Quiz Answers

Pop Quiz Answers

Pop Quiz Answers

Index