In this article, i will create a simple application that implent NHibernate, and in database side, i will use SQL Server 2008. What is NHibernate ? I will not explain it in there, so many explanation what it is in internet, like in
here.
You can download latest NHibernate version
here.
First, i will create a simple database that have just one table (and i added some record in there):
This is the DDL
2 | ACCID int identity(1,1) not null , |
3 | USERNAME varchar(50) null , |
5 | constraint PK_ACCOUNT primary key nonclustered (ACCID) |
Hmm.. what is Identity(1,1) mean ??? It is mean we will set ACCID attribute, the primary key, to become autonumbered, the value will be added automatically incrementing from 1, 2, 3, … and so on.
Next step, we will create simple solution in Visual Studio 2008 SP1 (remember … to connect to SQL Server 2008, your Visual Studio 2008 must be Service Pack 1 ).
Add some references
- NHibernate.dll (NHibernate library)
- NHibernate.ByteCode.LinFu.dll (for lazy loading)
- System.configuration
Test your connection with database using Server Explorer
Do not forget to copy the connection string…
For my example, my database connection string is : ]
Data Source=WIRTH;Initial Catalog=TestNHibernate;User ID=azer89;Password=azer89
Nexttt !!!! create an App.config file in your project (Just Right click your project, Add, New Item, and Application Configuration File)
Set your App.config like this :
01 | xml version = "1.0" encoding = "utf-8" ?>
|
04 | < section name = "hibernate-configuration" type = "NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> |
06 | < hibernate-configuration xmlns = "urn:nhibernate-configuration-2.2" > |
08 | < property name = "connection.provider" >NHibernate.Connection.DriverConnectionProviderproperty > |
09 | < property name = "connection.driver_class" >NHibernate.Driver.SqlClientDriverproperty > |
10 | < property name = "connection.connection_string" >Data Source=WIRTH;Initial Catalog=TestNHibernate;User ID=azer89;Password=azer89property > |
11 | < property name = "dialect" >NHibernate.Dialect.MsSql2008Dialectproperty > |
12 | < property name = "proxyfactory.factory_class" >NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFuproperty > |
13 | < mapping assembly = "NHibernateTest" /> |
15 | hibernate-configuration >
|
You must pay attention in property connection.connection_string and dialect in code above. Add a model class similar to Account Table, its fields must be similar too
Your Account.cs should be like this :
02 | using System.Collections.Generic; |
06 | namespace NHibernateTest |
11 | private string username; |
14 | public virtual int AccId |
17 | set { accId = value; } |
20 | public virtual string UserName |
22 | get { return username; } |
23 | set { username = value; } |
26 | public virtual string Pass |
After creating Account class, we will create map to Account Table using XML file, so NHibernate will know that Account class in application will map to Account Table in database. The XML filename will be “Account.nbm.xml”
01 | xml version = "1.0" encoding = "utf-8" ?>
|
02 | < hibernate-mapping xmlns = "urn:nhibernate-mapping-2.2" namespace = "NHibernateTest" assembly = "NHibernateTest" > |
03 | < class name = "Account" table = "ACCOUNT" lazy = "false" > |
05 | < column name = "ACCID" /> |
06 | < generator class = "native" /> |
08 | < property name = "UserName" > |
09 | < column name = "username" /> |
11 | < property name = "Pass" > |
Look at generator class, its value is “native”, it is because we have set ACCID column into autoincrement. Oh yeah, Build Action for hbm.xml file must be “Embedded Resource”
Next step is to create persistence class, we will name it NHibernateHelper.cs
01 | using System.Reflection; |
05 | namespace NHibernateTest |
07 | public sealed class NHibernateHelper |
09 | private static ISessionFactory SessionFactory; |
11 | private static void OpenSession() |
13 | Configuration configuration = new Configuration(); |
14 | configuration.AddAssembly(Assembly.GetCallingAssembly()); |
15 | SessionFactory = configuration.BuildSessionFactory(); |
18 | public static ISession GetCurrentSession() |
20 | if (SessionFactory == null ) |
21 | NHibernateHelper.OpenSession(); |
23 | return SessionFactory.OpenSession(); |
26 | public static void CloseSessionFactory() |
28 | if (SessionFactory != null ) |
29 | SessionFactory.Close(); |
Since ISessionFactory is expensive to create (it almost take one second in my computer !!!) we will create one object of ISessionFactory, otherwise with ISession, it is very cheap to create, so we can create it very ofter in your application. We will test the code :
02 | using System.Collections.Generic; |
07 | namespace NHibernateTest |
11 | static void Main( string [] args) |
13 | string username = "reza" ; |
16 | ISession session = NHibernateHelper.GetCurrentSession(); |
17 | IQuery query = session.CreateQuery( "FROM Account WHERE username = :name AND pass = :pass " ); |
18 | query.SetString( "name" , username); |
19 | query.SetString( "pass" , pass); |
20 | IList acc = query.List(); |
23 | Console.WriteLine( "Cannot find specified user" ); |
25 | Console.WriteLine( "Found " + acc[0].UserName); |
It is Work !!!
No comments:
Post a Comment