C#.NET编程Spring.NET & NHibernate整合

添加Spring.NET为NHibernate的容器配置
现在就可以在Spring.NET的容器中添加Nhibernate的配置了。
如下Spring_nhibernate.xml:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns='http://www.springframework.net'>

<!-- NHibernate初始化的 -->
<object id="DbProvider" type="OKEC.Sample.Spring.SQLProvider,SpringNHibernateSample">
    <property name="ConnectionString" value="Data Source=192.168.88.15;Database=liluhua;User ID=sa;Password=sa;Trusted_Connection=False"/>
</object>

<object id="SessionFactory"
type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="MappingAssemblies">
        <list>
            <value>SpringNhibernateSample</value>
        </list>
    </property>
    <property name="HibernateProperties">
        <dictionary>
            <entry
key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
            <!--entry
key="hibernate.connection.connection_string" value="Data Source=192.168.188.188;Database=Test;User ID=satest;Password=satest;Trusted_Connection=False"/-->
            <entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
            <entry
key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>  
        </dictionary>
    </property>
</object>

<object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="sessionFactory" ref="SessionFactory"/>
</object>

<object id="TransactionInterceptor"
type="Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data">
    <property name="TransactionManager" ref="HibernateTransactionManager"/>
    <property name="TransactionAttributeSource">
        <object
type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data"/>
    </property>
</object>

<!-- 以下是业务相关的 -->
<object id="UserDao"
type="OKEC.Sample.NHibernate.NHibernateTest.UserDao, SpringNHibernateSample">
        <property name="SessionFactory" ref="SessionFactory"/>
</object>
</objects>
我们现在对上面的加以细解:
下面这几行,是配置Nhibernate所需的数据库的DbProvider
<object id="DbProvider" type="OKEC.Sample.Spring.SQLProvider,SpringNHibernateSample">
    <property name="ConnectionString" value="Data Source=192.168.88.15;Database=liluhua;User ID=sa;Password=sa;Trusted_Connection=False"/>
</object>


下面的是对Nhibernate的SessionFactory的封装的对像的定义
<object id="SessionFactory"
type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="MappingAssemblies">
        <list>
            <value>SpringNhibernateSample</value>
        </list>
    </property>
    <property name="HibernateProperties">
        <dictionary>
            <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
            <!--entry key="hibernate.connection.connection_string" value="Data Source=192.168.188.188;Database=Test;User ID=satest;Password=satest;Trusted_Connection=False"/-->
            <entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
            <entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>  
        </dictionary>
    </property>
</object>


下面的是对Nhibernate中的Transaction封装对像
<object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="sessionFactory" ref="SessionFactory"/>
</object>

<object id="TransactionInterceptor" type="Spring.Transaction.Interceptor.TransactionInterceptor, Spring.Data">
    <property name="TransactionManager" ref="HibernateTransactionManager"/>
    <property name="TransactionAttributeSource">
        <object type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource, Spring.Data"/>
    </property>
</object>


下面是对NHibernate业务操作对像的定义

<object id="UserDao"
type="OKEC.Sample.NHibernate.NHibernateTest.UserDao, SpringNHibernateSample">
        <property name="SessionFactory" ref="SessionFactory"/>
</object>

原文地址:https://www.cnblogs.com/soundcode/p/1911847.html