Title: Dave Elliman
1Getting Started with NHibernate
2Designing an Enterprise Architecture
- Collect user stories
- Draw Use Case diagrams
- Decide on the multi-tier architecture to be used
(can defer client decision) - Produce the domain model
- This is the heart of your design
- The user stories will be a valuable source of
objects - Use design rules and proven patterns where
applicable
3The Use Case Diagram
4The Domain Model
- Please dont make me write that C.R.U.D. code!
- We can now code up the POCOs (POJOs in Java)
- Note that they are testable outside of any
framework.
5 Designing Database Tables
- Traditionally done by analysing the domain model
to 3NF - Create that tables
- Write a configuration file for each class and put
both in the same directory (lttablegt.hbc.xml) - Specify how NHibernate should access the database
another xml file. - Is this xml hell? (not compared to J2EE).
6Lets do an example
Persisting the Student Class
7From the domain model
using System using System.Collections.Generic us
ing System.Linq using System.Text namespace
ConsoleApplication1 class Student
public int id public string
surname public string firstnames
public string email public string
mobile public string SayHello()
return string.Format("'Hello world!', said
0.", surname)
8I created a database
9Creating a database table
10The Student Table
11Adding student.hbm.xml
12This defines the mapping
lt?xml version"1.0" encoding"utf-8" ?gt
lthibernate-mapping xmlns"urnnhibernate-mapping-
2.2" auto-import"true"gt ltclass
name"ConsoleApplication1.Student,
ConsoleApplication1" lazy"false"gt ltid
name"id" access"field"gt ltgenerator
class"native" /gt lt/idgt ltproperty
name"surname" access"field" column"surname"/gt
ltproperty name"firstnames" access"field"
column"firstnames"/gt ltproperty name"email"
access"field" column"email"/gt ltproperty
name"mobile" access"field" column"mobile"/gt
lt/classgt lt/hibernate-mappinggt
13Ineed an App.config file
lt?xml version"1.0" encoding"utf-8"
?gt ltconfigurationgt ltconfigSectionsgt ltsection
name"hibernate-configuration" type"NHibernate.Cf
g.ConfigurationSectionHandler, NHibernate" /gt
lt/configSectionsgt lthibernate-configuration
xmlns"urnnhibernate-configuration-2.2"gt
ltsession-factorygt ltproperty
name"connection.provider"gt
NHibernate.Connection.DriverConnectionProvider
lt/propertygt ltproperty name"connection.dr
iver_class"gt NHibernate.Driver.SqlClientDr
iver lt/propertygt ltproperty
name"connection.connection_string"gt
Serverlocalhost\SQLEXPRESSdatabaselearningInte
grated SecuritySSPI lt/propertygt
ltproperty name"dialect"gt
NHibernate.Dialect.MsSql2005Dialect
lt/propertygt ltproperty name"show_sql"gt
false lt/propertygt lt/session-factorygt
lt/hibernate-configurationgt lt/configurationgt
14Ineed an App.config file
lt?xml version"1.0" encoding"utf-8"
?gt ltconfigurationgt ltconfigSectionsgt ltsection
name"hibernate-configuration" type"NHibernate.Cf
g.ConfigurationSectionHandler, NHibernate" /gt
lt/configSectionsgt lthibernate-configuration
xmlns"urnnhibernate-configuration-2.2"gt
ltsession-factorygt ltproperty
name"connection.provider"gt
NHibernate.Connection.DriverConnectionProvider
lt/propertygt ltproperty name"connection.dr
iver_class"gt NHibernate.Driver.SqlClientDr
iver lt/propertygt ltproperty
name"connection.connection_string"gt
Serverlocalhost\SQLEXPRESSdatabaselearningInte
grated SecuritySSPI lt/propertygt
ltproperty name"dialect"gt
NHibernate.Dialect.MsSql2005Dialect
lt/propertygt ltproperty name"show_sql"gt
false lt/propertygt lt/session-factorygt
lt/hibernate-configurationgt lt/configurationgt
15Ineed an App.config file
lt?xml version"1.0" encoding"utf-8"
?gt ltconfigurationgt ltconfigSectionsgt ltsection
name"hibernate-configuration" type"NHibernate.Cf
g.ConfigurationSectionHandler, NHibernate" /gt
lt/configSectionsgt lthibernate-configuration
xmlns"urnnhibernate-configuration-2.2"gt
ltsession-factorygt ltproperty
name"connection.provider"gt
NHibernate.Connection.DriverConnectionProvider
lt/propertygt ltproperty name"connection.dr
iver_class"gt NHibernate.Driver.SqlClientDr
iver lt/propertygt ltproperty
name"connection.connection_string"gt
Serverlocalhost\SQLEXPRESSdatabaselearningInte
grated SecuritySSPI lt/propertygt
ltproperty name"dialect"gt
NHibernate.Dialect.MsSql2005Dialect
lt/propertygt ltproperty name"show_sql"gt
false lt/propertygt lt/session-factorygt
lt/hibernate-configurationgt lt/configurationgt
16Ineed an App.config file
lt?xml version"1.0" encoding"utf-8"
?gt ltconfigurationgt ltconfigSectionsgt ltsection
name"hibernate-configuration" type"NHibernate.Cf
g.ConfigurationSectionHandler, NHibernate" /gt
lt/configSectionsgt lthibernate-configuration
xmlns"urnnhibernate-configuration-2.2"gt
ltsession-factorygt ltproperty
name"connection.provider"gt
NHibernate.Connection.DriverConnectionProvider
lt/propertygt ltproperty name"connection.dr
iver_class"gt NHibernate.Driver.SqlClientDr
iver lt/propertygt ltproperty
name"connection.connection_string"gt
Serverlocalhost\SQLEXPRESSdatabaselearningInte
grated SecuritySSPI lt/propertygt
ltproperty name"dialect"gt
NHibernate.Dialect.MsSql2005Dialect
lt/propertygt ltproperty name"show_sql"gt
false lt/propertygt lt/session-factorygt
lt/hibernate-configurationgt lt/configurationgt
17Required libraries
using System using System.Reflection using
System.Collections.Generic using
System.Text using NHibernate using
NHibernate.Cfg
Same as import in Java
In VS2008 Right click on project name Select Add
Reference Select Browse tab Find
nhibernate.dll (similar to adding a .jar to
CLASSPATH) (you downloaded this from
http//sourceforge.net/projects/nhibernate
18Initialize NHibernate
namespace ConsoleApplication1 class Program
static ISessionFactory factory
static void CreateStudentAndSaveToDatabase()
Student bloggs new Student()
bloggs.surname Mole"
bloggs.firstnames Adrian"
bloggs.email axm06u_at_cs.nott.ac.uk"
bloggs.mobile "07865432566" using
(ISession session OpenSession())
using (ITransaction transaction
session.BeginTransaction())
session.Save(bloggs)
transaction.Commit()
Console.WriteLine("Saved Bloggs to
database")
19Initialize NHibernate
namespace ConsoleApplication1 class Program
static ISessionFactory factory
static void CreateStudentAndSaveToDatabase()
Student bloggs new Student()
bloggs.surname Mole"
bloggs.firstnames Adrian"
bloggs.email axm06u_at_cs.nott.ac.uk"
bloggs.mobile "07865432566" using
(ISession session OpenSession())
using (ITransaction transaction
session.BeginTransaction())
session.Save(bloggs)
transaction.Commit()
Console.WriteLine("Saved Bloggs to
database")
20What is OpenSession()?
static ISession OpenSession()
if(factory null)
Configuration c new Configuration()
c.AddAssembly(Assembly.GetCallingAssembly())
factory c.BuildSessionFactory()
return
factory.OpenSession()
21Only Main() left out
static void Main(string args)
CreateStudentAndSaveToDatabase()
22Whats the verdict?
- Quite a lot of work for one table
- But we can now do C.R.U.D. from simple method
calls like Load() Save() LoadAll() - Adding more tables is does not require more
configuration and initialization to be done again
just the ltfilegt.hbm.xml to be added - We are now Vendor independent
- And the code is maintainable
23We can do better still
- Code is even more maintainable if we use
attributes in the class definitions rather than
hbm.xml files - Some developers object code should be code not
configuration. - Having the code in one file and configuration in
another invites them to become out of sync. - I recommend using attributes
24Using attributes
Add a Reference to the .dll as well
using NHibernate.Mapping.Attributes
class(TableStudent) class Student
PrimaryKey public int id
property public string surname
property public string firstnames
property public string email
property public string mobile
Best practice is to make the field private and
add get and set methods
If certain naming conventions and defaults are
used this can involve little typing
Tools are then available to write The
configuration files
25Any Questions