zoukankan      html  css  js  c++  java
  • NHibernate测试的几个小问题

    在使用NHibernate时,我发现有许多陷阱,看似微不足道,如果不明白,就会阻碍我们的开发,乃至于影响到开发效率,成为开发的拦路虎。

    1、首先是映射的实体类,例如Customer类。由于我采用DDD的方式,将领域逻辑也放入到该实体类中,且通过构造函数传入了一个Repository对象,代码如下:

    public class Customer {
        private ICustomerRepository m_repository;
        public virtual int CustomerID {
            get;
            set;
        }
        public virtual string Name {
            get;
            set;
        }
        public virtual DateTime Birthday {
            get;
            set;
        }
        public virtual string Address {
            get;
            set;
        }
        public Customer(ICustomerRepository repository) {
            m_repository = repository;
        }
        public Customer Load(int customerID) {
            return m_repository.Load(customerID);
        }
    }

    这样的定义会导致无法进行Mapping,会抛出NHibernate.InvalidProxyTypeException异常。原因在于如果实体类定义了一个带参的构造函数,则必须显式地定义一个无参的构造函数。此外,定义在Customer类中的方法,同样必须加上virtual关键字。

    public Customer() {
    }
    public virtual Customer Load(int customerID) {
        return m_repository.Load(customerID);
    }

    2、如果使用Visual Studio Team Suite自带的测试框架,则会带来无法找到hibernate.cfg.xml文件的问题。在一般的测试框架下,我们可以将该文件的Copy to output directory属性设置为“copy always”即可。但由于VSTS自带的测试框架会将相关文件放到自动生成的TestResult下的临时文件夹中。因此,可能会抛出如下的异常:

    NHibernate.Cfg.HibernateConfigException: An exception occurred during configuration of persistence layer. --->  System.IO.DirectoryNotFoundException: 未能找到路径“C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies;PrivateAssemblies\hibernate.cfg.xml”的一部分。

    一个简单的解决方案时将hibernate.cfg.xml拷贝到TestResult目录下,并将构建SessionFactory对象的方法修改为:

    ISessionFactory factory = new Configuration().Configure(@"./../../hibernate.cfg.xml").BuildSessionFactory();

    好在这只是为了测试而做,所以方案变得如此丑陋,也是可以接受的。

    3、关于hbm文件。一般来说,我们需要将实体对象的hbm文件例如customer.hbm.xml文件的Build Action属性设置为Embedded Resource即可。若要验证该属性的设置是否生效,可以通过Reflector查看该程序集下的Resource。如下图: nhibernatereflector

    然而,如果hibernate.cfg.xml的设置有错,仍然会抛出如下异常:NHibernate.MappingException : No persister for: DomainLayer.Entities.Customer。

    我们需要在hibernate.cfg.xml文件中增加<mapping>:

    <?xml version="1.0" encoding="utf-8" ?>
      <hibernate-configurationxmlns="urn:nhibernate-configuration-2.2" >
      <session-factory name="NHibernate.Test">
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.connection_string">
          Data Source=.\SQLEXPRESS;Initial Catalog=EBusiness;
          Integrated Security=True;Pooling=False
        </property>
        <property name="adonet.batch_size">10</property>
        <property name="show_sql">true</property>
        <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
        <property name="use_outer_join">true</property>
        <property name="command_timeout">60</property>
        <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
        <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
        <mapping assembly="DomainLayer"/>
      </session-factory>
    </hibernate-configuration>
  • 相关阅读:
    支付业务的数据库表的设计
    数据库设计的一些参考(来源网上的一些帖子,仅供作者本人参考)
    Delphi 10.2的 更新说明,所有官方资料:新特征和Bugfix列表,所有工具开发说明
    日本太笨了,以小目标发动大战争,而且偷袭时候如小贼入室,一旦得手就仓皇逃窜
    理论经典:TCP协议的3次握手与4次挥手过程详解
    C++没有库则寸步难行,有库则几乎可以做任何事情——Bjarne Stroustrupi
    Ruby已经慢慢走向衰退了,那些年代久远而且小众的语言没有翻身的可能性
    前端面试题精选
    Spring MVC 基础
    NET WEB
  • 原文地址:https://www.cnblogs.com/wayfarer/p/1728418.html
Copyright © 2011-2022 走看看