NHibernate 3 Beginner's Guide

Rapidly retrieve data from your database into .NET objects

NHibernate 3 Beginner's Guide

Beginner's Guide
Dr. Gabriel Nicolas Schenker, Aaron Cure

Rapidly retrieve data from your database into .NET objects
$10.00
$44.99
RRP $26.99
RRP $44.99
eBook
Print + eBook
$12.99 p/month

Get Access

Get Unlimited Access to every Packt eBook and Video course

Enjoy full and instant access to over 3000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.

+ Collection
Free Sample

Book Details

ISBN 139781849516020
Paperback368 pages

About This Book

  • 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

Who This Book Is For

If you are a new or seasoned developer of .NET web or desktop applications who is looking for a better way to access database data, then this book is for you. 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. Absolutely no knowledge of NHibernate is required to read this book.

Table of Contents

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
Summary
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
Summary
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
The order entry model
Time for action – Implementing an order entry model
Summary
Chapter 4: 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
Chapter 5: 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
Chapter 6: 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
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
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
Chapter 8: 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
Chapter 9: 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
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?
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
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
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

What You Will Learn

  • 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

In Detail

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.

Authors

Table of Contents

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
Summary
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
Summary
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
The order entry model
Time for action – Implementing an order entry model
Summary
Chapter 4: 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
Chapter 5: 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
Chapter 6: 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
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
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
Chapter 8: 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
Chapter 9: 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
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?
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
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
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

Book Details

ISBN 139781849516020
Paperback368 pages
Read More