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(@"./http://www.cnblogs.com/hibernate.cfg.xml").BuildSessionFactory();

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

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


    然而,如果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>

  • 相关阅读:
    海云健康:上云为10万家药店带去了什么价值?
    PostgreSQL数据目录深度揭秘
    当设计模式遇上 Hooks
    SpringBoot 优雅停止服务的几种方法
    【2021-08-19】连岳摘抄
    【2021-08-18】该休息,还是要好好休息
    【2021-08-17】心里没底,就给它得找个底
    NFLSOJ 1072
    Solution -「ARC 125F」Tree Degree Subset Sum
    Solution -「ARC 125E」Snack
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1788617.html
Copyright © 2011-2022 走看看