NHibernate is a framework that allows us to talk to a relational database in an object-oriented way. We can store (or as we also often say, “persist”) objects in a database and load those objects from the database later on. NHibernate “auto-magically” translates our object-based language to a language that the database understands. That is, NHibernate generates the necessary SQL statements for us to insert, update, delete, and load data.
the database schema is a description of the structure of the database, and as such, describes how the data is organized inside the database and is divided into database tables. The schema also contains specifications about the type of data that can be stored, the relations of the database tables among each other, and the access strategies selected when accessing the data. In a relational database, a schema defines tables, fields or columns, relationships, indexes, constraints, views, and stored procedures, among others.
Chapter 1: 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
Chapter 2: 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
Chapter 3: 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
Owns or contains
The order entry model
Time for action – Implementing an order entry model
Chapter 4: Defining the Database Schema
What is a database schema
Who owns the database
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
Data types of 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
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
Chapter 5: Mapping the Model to the Database
What is mapping?
Types of mapping
Mapping by convention
A word about lazy loading
Loading data on demand
Virtual properties and methods
Creating database schema creation scripts
Expression trees – please explain
Entity level settings
Mapping many-to-many relations
Mapping value objects
Time for action – Mapping our domain
Use mapping conventions
Foreign key conventions
No mapping; is that possible?
Auto-mapping with Fluent NHibernate
Time for action – Using auto-mapping
Time for action – Using ConfORM to map our domain
Mapping value objects
Time for action – Mapping a simple domain using XML
Chapter 6: Sessions and Transactions
What are sessions and transactions
The session factory
Creating your first session
Why do we call Commit?
Adding new data to the database
Reading data from the database
Get versus Load
Updating existing data
First level cache or identity map
Clearing the cache
Refreshing entities in the cache
No database operation without a transaction
Should I use transactions when querying data?
NHibernate session versus database session
Time for action – Creating a session and doing some CRUD
Time for action –Implementing session management for a web application
WinForm or WPF applications
Unit of Work
Second level cache
Second level cache implementations
Time for action – Using a second level cache
Chapter 7: Testing, Profiling, Monitoring, and Logging
Why do we need tests?
What should we test?
What about the database?
Preparing our environment for testing
Testing the mapping
Testing the mapping with Fluent NHibernate
Time for action – Creating the base for testing
Time for action – Using SQLite in our tests
Why do we need to log?
Logging with Log4Net
Time for action – Adding logging to our application
Setting up logging for NHibernate
Time for action – Enable logging in NHibernate
Monitoring and profiling
Analyzing log files
Using SQL Server Profiler
Monitoring and profiling with NHibernate Profiler
Time for action – Adding NHibernate Profiler support
Chapter 8: Configuration
Why do we need a configuration?
Elements of the configuration
Which database do we want to use?
What byte code provider and proxy factory?
Where are our mappings?
Do we use second level caching?
Do we want to extend NHibernate?
Time for action – Configuring NHibernate using XML
Configuring NHibernate in code
Time for action – Configuring NHibernate in code
Configuring NHibernate with Loquacious
Time for action – Using Loquacious to configure NHibernate
Configuring NHibernate with Fluent NHibernate
Convention over configuration
Chapter 9: Writing Queries
How can we get to our data?
The LINQ to NHibernate provider
Defining the root of our query
Limiting the number of records returned
Filtering a set of records
Mapping a set of records
Sorting the resulting set
Forcing a LINQ query to execute immediately
Changing from querying the database to querying in-memory objects
Creating a report using LINQ to NHibernate
Time for action – Preparing the system
Time for action – Creating the reports
Untyped criteria queries
Strongly-typed 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
Chapter 10: 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?
Where to validate?
Validating single properties
Configuring the validator
Defining validation rules
Fluently configure validation rules
Time for action – Using property validation
Validating complex business rules
Enforcing always valid entities
Using validation classes
Time for action – Validating user input
What to do if we don’t own the database?
Chapter 11: 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
The select (n+1) problem
Accessing lazy loaded parts after the session is closed
Did I just load the whole database?
Using one model for read and write operations
Time for action – Causing a phantom update
Using NHibernate to persist any type of data
NHibernate 3 Beginner’s Guide