Nhibernate kick start!

 

很早就听说了Hibernate的大名没想到.net 的爱好者们早已把他移植到.net framework下了,最近使用了一下,和在java平台下的感觉一样,但是由于资料不足在配置时还是遇到一些麻烦,还是把自己的心得写下啦和大家分享一下!

 

Hardware

cpu intel E6500

memory 2G

hhd wd500g

video Gt210

 

Software:

OS: windows7

Dev: vs2010 beta2

Nhibernate:NHibernate-2.1.2.GA

Npgsql:Npgsql2.0.9-bin-ms.net

Postgre: 8.4.3 on vmware Ubuntu 10.4LTS

 

软硬件环境就是如此,下面开始切入正题。

我还是从Nhibernate一篇官方教程开始,可以参见(http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application.aspx

首先创建一个工程FirstSolution选择Class library作为一个程序集。Assembly Name FirstSolution

Default namespace: FirstSolution

Target Framework:.Net Framework 3.5

目标Framework一定要改成3.5,因为vs2010默认是4.0但是现在的Nhibernate的版本不支持4.0每次编译都会出问题。Release版也要做同样的设置。

 

 

在另一个标签页Build也要修改一下设置:

Output path: 修改为../bin/Debug

Release版也要做同样的设置。

 

 

 

构建工程:

1.  FirstSolution工程下添加三个引用:

Nhibernate,

Nhibernate.ByteCode.Linfu,

Npgsql

 

三个引用的属性一定要修改成 Copy LocalTrue

 

 

2.  添加类:

IproductRepository :一个接口用来操作Entity

using System;

using System.Collections.Generic;

 

namespace FirstSolution.Domain

{

    public interface IProductRepository

    {

        void Add(Product product);

        void Update(Product product);

        void Remove(Product product);

        Product GetById(Guid productId);

        Product GetByName(string name);

        ICollection<Product> GetByCategory(string category);

    }

}

 

ProductRepository :实现上面的接口,我只实现了一个Add方法

using System;

using System.Collections.Generic;

using FirstSolution.Domain;

using NHibernate;

 

namespace FirstSolution.Repositories

{

    public class ProductRepository : IProductRepository

    {

        public void Add(Product product)

        {

            using (ISession session = NHibernateHelper.OpenSession())

            using (ITransaction transaction = session.BeginTransaction())

            {

                session.Save(product);

                transaction.Commit();

            }

        }

       

        public void Update(Product product)

        {

            throw new NotImplementedException();

        }

       

        public void Remove(Product product)

        {

            throw new NotImplementedException();

        }

       

        public Product GetById(Guid productId)

        {

            throw new NotImplementedException();

       

        }

       

        public Product GetByName(string name)

        {

            throw new NotImplementedException();

        }

       

        public ICollection<Product> GetByCategory(string category)

        {

            throw new NotImplementedException();

        }

    }

}

 

NhibernateHelper:一个Help类,用来创建SessionFactory

using FirstSolution.Domain;

using NHibernate;

using NHibernate.Cfg;

namespace FirstSolution.Repositories

{

    public class NHibernateHelper

    {

        private static ISessionFactory _sessionFactory;

        private static ISessionFactory SessionFactory

        {

            get

            {

                if (_sessionFactory == null)

                {

                    var configuration = new Configuration();

                    configuration.Configure();

                    configuration.AddAssembly(typeof(Product).Assembly);

                    _sessionFactory = configuration.BuildSessionFactory();

                } return _sessionFactory;

            }

        }

        public static ISession OpenSession()

        {

            return SessionFactory.OpenSession();

        }

    }

}

 

Product :Entiy 类用来做映射用的。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace FirstSolution.Domain

{

    public class Product

    {

        public virtual Guid Id

        {

            get;

            set;

        }

        /// <summary>

        /// Name

        /// </summary>

        public virtual string Name

        {

            get;

            set;

        }

       

        /// <summary>

        /// Category

        /// </summary>

        public virtual string Category

        {

            get;

            set;

        }

 

        /// <summary>

        /// Discontinued

        /// </summary>

        public virtual bool Discontinued

        {

            get;

            set;

        }

    }

}

 

 

3.添加映射XML文件。

在工程中添加Product.hbm.xml一定要将其属性修改一下。

Build Action:改为Embedded Resource.

Copy To output Directory 改为 Copy always

Xml的需要修改成如下内容:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="FirstSolution" namespace="FirstSolution.Domain">

  <class name="Product">

    <id name="Id">

      <generator class="guid" />

    </id>

    <property name="Name" />

    <property name="Category" />

    <property name="Discontinued" />

  </class>

</hibernate-mapping>

注意:schemas选项中要选种/NHibernate-2.1.2.GA-bin/Required_Bins/nhibernate-mapping.xsd这个文件,这样xml错误了可以马上校验。

在工程中添加hibernate.cfg.xml

修改属性Build Action:修改为Content

修改内容如下:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">

  <session-factory>

    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>

    <property name="dialect">NHibernate.Dialect.PostgreSQL82Dialect</property>

    <property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>

    <property name="connection.connection_string">Server=192.168.193.128;Port=5432;Database=test;User ID=postgres;Password=8888888;</property>

    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>

    <!--<mapping assembly="FirstSolution" />-->

  </session-factory>

 

</hibernate-configuration>

需要注意的是,Dailect属性对于postgre最好使用NHibernate.Dialect.PostgreSQL82Dialect,否则不能支持Guid这种类型。这是Nhibernate的一个BUG,有兴趣可以上网查一下。

 

最后,创建一个工程NhibernateTest作为入口程序入口,喜欢win Form也好,WPF甚至像教程中使用Test方法也可以。只要调用了前面程序集的方法Nhibernate就开始工作了。

 

 

需要源码的programer 可以留下自己的email.我看到后会尽快奉上!

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值