[Study Note] NHibernate In Action 20100521

NHibernate In Action

Part 1 Discovering ORM with NHibernate

1 Object/relational persistence in .NET

ORM object/relational mapping

1.1 What is persistence?

1.1.1 Relational database
1.1.2 Understanding SQL

SQL Tuning by Dan Tow [Tow 2003]
SQL Cookbook by Anthony Molinaro [Mol 2005]
An Introduction to Database Systems [Date 2004]

1.1.3 Using SQL in .NET application

Patterns of Enterprise Application Architecture by Martin Fowler [Fowler 2003]

1.1.4 Persistence in object-oriented applications

persistent object
transient object

[GOF 1995]

1.1.5 Persistence and the layered architecture

Presentation layer
Business layer
Persistence layer
Database
Helper/utility classes

1.2 Approaches to persistence in .NET

1.2.1 Choice of persistence layer

(p9 2010-05-20 17:15)

hand-coded persistence layer
dataset-based persistence layer
NHibernate persistence layer
LINQ to SQL-based persistence layer
ADO.NET Entity Framework

1.2.2 Implementing the entities

hand-coded entities
entities in a dataset
entities and NHibernate
entities and LINQ to SQL

paradigm mismatch / object/relational impedance mismatch

1.2.3 Displaying entities in the user interface

Dataset-based persentation layer
Persentation layer and entities

ObjectViews

1.2.4 Implementing CRUD operations

hand-coded CRUD operations
CRUD operations with datasets
CRUD operations using NHibernate
LINQ to SQL CRUD operations

table adapters

1.3 Why do we need NHibernate ?

productivity, maintainability and performance

1.3.1 The paradigm mismatch

The problem of granularity
The problem of inheritance and polymorphism
The problem of identity
problem relating to associations

primary key / surrogate key

1.3.2 Units of work and conversations

conversation / business transactions / application transactions

The Unit of Work Pattern
The Identity Map pattern
Transparent persistence and lazy loading
Caching

1.3.3 Complex queries and the ADO.NET Entity Framework

Implementing a query engine

HQL - Hibernate Query Language
QBC - Query by Criteria API

ADO.NET Entity Framework

1.4 object/relation mapping

1.4.1 what is ORM

object/relational mapping is the automated (and possibly transparent) persistence of objects in an application to the tables in a relational database, using metadata that describes the mapping between the objects and the database.

ORM, in essence, works by transforming data from one representation to another.

entity-relationship modeling, ERM
object role modeling, ORM

1.4.2 why ORM ?

modeling mismatch
productivity and maintainability
performance
database independence

(p24 2010-05-20 18:20)

2 Hello NHibernate

2.1 "Hello World" with NHibernate

2.1.1 Installing NHibernate
2.1.2 Create a new Visual Studio project
2.1.3 Creating the Employee class
2.1.4 Setting up the database
2.1.5 Creating an Employee and saving to the database
2.1.6 Loading an Employee from the database
2.1.7 Creating a mapping file
2.1.8 Configuring your application
2.1.9 Updating an Employee
2.1.10 Running the program

The persistent class can be used with or without NHibernate

Enbedded Resource

automatic dirty checking
cascading save
transactional write-behind

2.2 Understanding the architecture

2.2.1 The core interface

ISession interface
ISessionFactory interface
Configuration interface
ITransaction interface
IQuery and ICriteria interface

An instance of ISession is lightweight and is inexpensive to create and destroy.

2.2.2 Callback interface

Callback interfaces allow the application to receive a notification when something interesting happens to an object.

ILifecycle interface
IValidatable interface

persistence lifecycle

2.2.3 Types

An NHibernate Type object maps a .NET type to a database column type (the type may span multiple columns).

custom types

IUserType, ICompositeUserType, IParameterizedType, IUserCollectionType

2.2.4 Extension interface

Primary-key generation (IIdentifierGenerator interface)
SQL dialect support (Dialect abstract class)
Caching strategies (ICache and ICacheProvider interface)
ADO.NET connection management (IConnectionProvider interface)
Transaction management (ITransactionFactory and ITransaction interface)
ORM strategies (IClassPersister interface hierarchy)
Property-access strategies (IPropertyAccessor interface)
Proxy creation (IProxyFactory interface)

2.3 Basic configuration

2.3.1 Creating a SessionFactory

Method chaining

Method chaining is a programming style supported by many NHibernate interfaces (they're also called fluent interfaces).

If you use the coding style of Method chaining, it's better to write each method invocation on a different line.

one mapping file per class

working with mapping files

Multiple database and session factories

Configuration techniques

2.3.2 Configuring the ADO.NET database access

Configuring NHibernate using hibernate.cfg.xml

  • connection.provider
  • dialect
  • connection.driver_class
  • connection.connection_string

Starting NHibernate

  1. If your database's ADO.NET data provider isn't ye installed, download and install it.
  2. Add log4net.dll as reference to your project.
  3. Decide which database-access properties NHibernate will need.
  4. Let the Configuration know about these properties by placing them in a hibernate.cfg.xml file in the current directory.
  5. Create an instance of Configuration in your application, call the Configure() method; load the mapped class ( with .NET attributes ) using HbmSerializer.Default.Serialize() and AddInputStream(); and load the XML mapping files using either AddAssembly(), AddClass(), or AddXmlFile(). Build an ISessionFactory instance from the Configruation by calling BuildSessionFactory().
  6. Remember to close the instance of ISessionFactory( using MySessionFactory.Close() ) when you're done using NHibernate.

2.4 Advanced configuration settings

2.4.1 Using the application configuration file

When configure() is called, NHibernate first searches for the information in the application configuration file and then in a file named hibernate.cfg.xml in the current directory.

<session-factory name="MySessionFactory">

NHibernate.Impl.SessionFactoryObjectFactory.GetNamedInstance()

2.4.2 Loging

write-behind

posted on 2010-05-22 23:34  zhaorui  阅读(198)  评论(0编辑  收藏  举报

导航