NHibernate Part 1

  • 1431
  • 0
  • C#
  • 2012-03-19

NHibernate Part 1

在.NET平台上我們也可以使用NHibernate ORM工具,此開源框架源自元Java的Hibernate。

接下來將發表一些導入NHibernate ORM Solution過程中遇到的心得。

大胖使用NHibernate理由:

  1. 此ORM框架在Java被使用多年,無法估計有多少專案使用,框架穩定度高。
  2. 用了它,改天寫Java比較不會適應不良( 這理由真爛 ),應該說網路上有很多Hibernate資源可供參考。

 

因為NHibernate剛開始主要是配製設定檔上面會花一些時間,所以就整理一下心得,方便日後參考。

  • 下載NHibernate And Install it
  • 建立專案
  • 配置設定檔

 

下載NHibernate路徑如下:

http://nhforge.org/Default.aspx

圖片 004

二、建立方案(NHibernateMy)、專案(HibernateMy1) console類型、專案(Model) 類別庫類型,如下圖:

圖片 005

2.1 分別在Model專案內新增Config、Domain、Mappings三個資料夾,接著說明每個資料夾內要新增的檔案。

Config 資料夾內是用來存放DB相關資訊設定檔,請在資料夾內新增hibernate.cfg.xml檔,內容如下

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: - <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
   3:     - <session-factory name="Model">
   4:         <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
   5:         <property name="connection.connection_string">Data Source=.\SQLExpress;Initial Catalog=MVC3.Models.MusicStoreDB;Integrated Security=True</property>
   6:         <property name="adonet.batch_size">10</property>
   7:         <property name="show_sql">false</property>
   8:         <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
   9:         <property name="command_timeout">60</property>
  10:         <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
  11:     </session-factory>
  12: </hibernate-configuration>

 

備註:

  • 其實這個設定檔案,可在下載回來的NHibernate壓縮檔解壓縮後,在Configuration_Templates資料夾內,找到相關模板範例。
  • 請把這個檔案複製到執行程式同一目錄。

2.2 在Domain資料夾內新增Album.cs , 內容如下

   1:  
   2: namespace Model.Domain
   3: {
   4:     public class Album
   5:     {
   6:         public virtual Guid AlbumId { get; set; }
   7:         public virtual int GenreId { get; set; }
   8:         public virtual int ArtistId { get; set; }
   9:         public virtual string Title { get; set; }
  10:         public virtual decimal Price { get; set; }
  11:         public virtual string AlbumArtUrl { get; set; }
  12:     }
  13: }

 

2.3 在Mappings資料夾內新增Album.hbm.xml,內容如下

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
   3:                    assembly="Model"
   4:                    namespace="Model.Domain">
   5:     <class name="Model.Domain.Album" table="Album2">
   6:         <id name="AlbumId">
   7:             <generator class="guid" />
   8:         </id>
   9:         
  10:         <property name="GenreId" />
  11:         <property name="ArtistId" />
  12:         <property name="Title" />
  13:         <property name="Price" />
  14:         <property name="AlbumArtUrl" />
  15:     </class>
  16:  
  17: </hibernate-mapping>

記得要將設定檔設為"內嵌資源"

圖片 006

 

2.4 新增NHibernateHelper.cs至Model專案底下

   1: using Model.Domain;
   2: using NHibernate;
   3: using NHibernate.Cfg;
   4:  
   5: namespace Model
   6: {
   7:     public class NHibernateHelper
   8:     {
   9:         private static ISessionFactory _sessionFactory;
  10:  
  11:         private static ISessionFactory SessionFactory
  12:         {
  13:             get
  14:             {
  15:                 if (_sessionFactory == null)
  16:                 {
  17:  
  18:                     var configuration = new Configuration();
  19:                    
  20:                     configuration.Configure();
  21:  
  22:                     //In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.
  23:                     configuration.AddAssembly(typeof(Album).Assembly);
  24:  
  25:                     _sessionFactory = configuration.BuildSessionFactory();
  26:  
  27:                 }
  28:                 return _sessionFactory;
  29:             }
  30:         }
  31:  
  32:         public static ISession OpenSession()
  33:         {
  34:             return SessionFactory.OpenSession();
  35:         }
  36:     }
  37: }

2.5 新增AlbumRepository.cs至Model專案下,只寫了一個Add Method之後有需要再加

   1: using Model.Domain;
   2: using NHibernate;
   3:  
   4: namespace Model
   5: {
   6:     public class AlbumRepository
   7:     {
   8:         public  void Add(Album album)
   9:         {
  10:             using (ISession session = NHibernateHelper.OpenSession())
  11:             using (ITransaction transaction = session.BeginTransaction())
  12:             {
  13:                 session.Save(album);
  14:                 transaction.Commit();
  15:             }
  16:         }
  17:     }
  18: }

 

補充說明:

由於我們是要透過Model專案來與DB做資料存取,所以記得要把NHibernate.dll專案參考加上。

三、在HibernateMy1專案的program.cs檔案新增如下內容

   1: using Model;
   2: using Model.Domain;
   3:  
   4: namespace HibernateMy1
   5: {
   6:     class Program
   7:     {
   8:         static void Main(string[] args)
   9:         {
  10:             Album album = new Album();
  11:            
  12:             album.ArtistId = 33;
  13:             album.GenreId = 44;
  14:             album.Title = "Dapon NHibernate";
  15:             
  16:             AlbumRepository albumRepository = new AlbumRepository();
  17:             albumRepository.Add(album);
  18:         }
  19:     }
  20: }

四、結果如下

圖片 007

 

PS.其實在導入過程中,不管是參考黃偉榮筆記orNHibernate 官網新手指導,都可以發現Code First的編程方式。

     (大胖還在持續適應這種開發模式哩~~~~~)微笑