NHibernate - Google Project Hosting

shrubberystatuesqueΔιαχείριση Δεδομένων

1 Δεκ 2012 (πριν από 4 χρόνια και 11 μήνες)

199 εμφανίσεις

NHibernate



an mature, object
-
relational mapping (ORM) solution for the Microsoft .NET platform



it provides a framework for mapping an object
-
oriented domain model to a traditional
relational database.



relieve the developer from a significant portion of
relational data persistence
-
related
programming tasks



free as open source software that is distributed under the GNU Lesser General Public
License.



port of the popular Java O/R mapper Hibernate to .NET



generates the SQL commands and relieves the developer
from manual data set handling and
object conversion



keeps the application portable to most SQL databases



database portability delivered at very little performance overhead.



Mappings



pokaži samo Product



download
http://sourceforge.net/projects/nhibernate/files/NHibernate/



look at the files



solution structure and Lib folder



Solution folder
Schema


o

nhibernate
-
configuration.xsd


o

nhibernate
-
mapping.xsd



mapping file is an XML
file wi
th
extension
.hbm.xml



set Build Action to
Embedded Resource



provide IntelliSense while completing mappings



The
model

is the collection of classes that will

be persisted in the database.



A
persistent class

is a
ny class that will be persisted (e.g. Person,
Address)



An
entity class

is a persistent class with an ID


o

e.g. Address will be persisted as part of Person, no ID
--

> not an entity class



An instance of an entity class is called an

entity
.



Plain Old CLR Object (POCO)

o

http://en.wikipedia.org/wiki/Plain_Old_CLR_Object

o

term is used to contrast a
simple object

with one that is designed to be used with a
complicated, special object frameworks such as an

ORM

component. Another way to
put it is that POCOs are objects unencumbered with inheritance or attributes needed
for specific frameworks



strongly held design de
cisions in NHibernate
:

o

all entity classes should be
persistence ignorant
, that is, they should not know about,
or be dependent on NHibernate



The
Id

property contain the primary key value from the database



persistent object identifier (POID)



primary key
uniquely identifies a row in a database table



the POID will uniquely identify an entity in memory



public virtual Guid Id { get; protected set; }



limit access to the
Id
property. Code outside of the
Product
class is unable to change the
value of the
Id
prop
erty. However, NHibernate sets properties using highly optimized
reflection, ignoring the
protected
restriction. This keeps your application from inadvertently
altering this value.



a single
hibernate
-
mapping
element



The
xmlns
attribute sets the XML namespa
ce, schema in our
Schema
folder, Visual Studio
uses this to enable IntelliSense



The
assembly
attribute tells NHibernate which assembly, by default, contains our types.



Similarly, the
namespace
attribute sets the default .NET namespace



simple name
Product
instead of the full assembly qualified name of
Eg.Core.Product
,
Eg.Core



a
class
element. The
name
attribute tells NHibernate that this
class
element defines the mapping for
our entity class
Product



The
Id
element defines the POID. The
name
attribute refers

to the
Id
property of our
Product
class



The
generator
element defines how NHibernate will generate POIDs. In this case, we've told
NHibernate to use the
guid.comb
algorithm. Several other options exist.



The
property
elements define properties on our
Produ
ct
class



name
attribute matches the name of a property



By default, NHibernate allows null values



Avoid redundant mappings



keep your mappings as short and concise as possible



NHibernate intelligently scans your model and combines this knowledge with the information provided in
the mapping. In most cases, specifying the types of properties in your mappings only creates redundancies
that must be maintained. The default table name

matches the class name, and each column name matches
the corresponding property by default.



There are three main approaches to begin developing an NHibernate application

o

With the model
-
first approach

o

The configuration
-
first approach

o

The database
-
first ap
proach is only suggested when sharing an existing database with
another application.



Startup
-

NHibernate deserialize XML mappings into a graph of hibernate mapping objects


What happens to these mappings?



combines this data with reflected metadata from th
e entity classes



we get
mapping metadata

-

contains everything NHibernate must know about our model








Surrogate keys and natural Ids




A
natural key
is an ID that has semantic meaning or business value

(It "means something" to people
in the real world
, A natural id is a property or set of properties that is unique and not null
)



A
surrogate key
is a
system generated

ID that has
no semantic meaning

(It is just a value that
uniquely identifies data in a database table, NHibernate strongly encourages the
use of surrogate
keys. There are two reasons for this)

o

First, the use of natural keys inevitably leads to the use of
composite keys
(
multi
-
field keys
composed of the natural keys of other objects
, This quickly leads to an incredible amount of
complexity
)

o

S
econd, because natural keys have real
-
world meaning, they must be allowed to change with
the real world

<natural
-
id
mutable
="true">


<property name="UserName" not
-
null="true" />

</natural
-
id>



m
utable
. default is
false
, this natural id are immutable, or constant.



allow our application to change the
UserName
of an account from time
-
to
-
time, so we set
mutable
to
true
.



natural id will create a unique database index on
UserName

ID generator selection


Non
-
insert POID gene
rators




best option for new applications



assign an identity to a persistent object without writing the object's data to the database



allowing NHibernate to delay writing until the business transaction is complete, reducing round trips
to the database




hilo

-

Hi/Lo algorithm
-

http://stackoverflow.com/questions/282099/whats
-
the
-
hi
-
lo
-
algorithm



guid

-

Using regular guids as a primary key value leads to fragmented indexes (due to the randomness of the
guid's value) which leads to bad
perfor
mance



guid.comb

-

combines 10 bytes of a seemingly
-
random GUID, with six bytes representing the current
date and time to form a new GUID
,
enerates sequential guids which solves the fragmented index issue



Using The Guid.Comb Identifier Strategy



The Cost of GUIDs as Primary Key
s



guid.native
gets a GUID from the database



uuid.hex
generates a GUID and stores it as a human
-
readable string of 32 hex digits with or without
dashes



uuid.string
generates a GUID, converts each of the GUID's 16 bytes to the binary equivalent
character, an
d stores the resulting 16 characters as a string. This is not human readable



counter
(also known as
vm
) is a simple incrementing integer. It's initialized from the system clock
and counts up. It's not appropriate for shared
-
database scenarios



increment
is
also a simple incrementing integer. It's initialized by fetching the maximum primary key
value from the database at start
-
up. It's not appropriate for shared
-
database scenarios



sequence
fetches a single new ID from a database that supports named sequences,

such as Oracle,
DB2, and PostgreSQL. Each generation requires a round trip to the database.
seqhilo
provides
better performance.



seqhilo
combines the Hi/Lo algorithm and sequences to provide better performance over the
sequence generator

Post
-
insert POID
generators



require data to be persisted to the data
base for an ID to be generated



al
ters the behavior of Nhibernate,
disables some performance features



use of these POID generators is strongly discouraged

(
only be used with existing databases where
other
app
lications rely on this behavior)




identity
returns a database
-
generated ID



select
performs a SELECT to fetch the ID from the row after the insert. It uses the natural id to find
the correct row.



sequence
-
identity
returns a database
-
generated ID for data
bases that support named sequences



trigger
-
identity
returns an ID generated by a database trigger.



native
generator maps to a different POID generator, depending on the database product

o

Microsoft SQL Server, DB2, Informix, MySQL, PostgreSQL, SQLite, and Sybase, it is equivalent
to
identity
.

o

Oracle and Firebird, it's the same as
sequence
.

o

Ingres, it's
hilo
.

Page 13