本文介绍了单独项目中的nHibernate域模型和映射文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以将域对象和映射文件分离到两个单独的项目中?我想创建一个包含我的域模型的名为MyCompany.MyProduct.Core的项目,以及另一个包含我的Oracle数据映射的名为MyCompany.MYProduct.Data.Oracle的项目.但是,当我尝试对此进行单元测试时,出现以下错误消息:

Is there a way to separate out the domain objects and mapping files into two separate projects? I would like to create one project called MyCompany.MyProduct.Core that contains my domain model, and another project that is called MyCompany.MYProduct.Data.Oracle that contains my Oracle data mappings. However, when I try to unit test this I get the following error message:

这是我的映射文件:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MyCompany.MyProduct.Core"
                   namespace="MyCompany.MyProduct.Core"
                   >
  <class name="MyCompany.MyProduct.Core.Client" table="MY_CLIENT" lazy="false">
    <id name="ClientId" column="ClientId"></id>
    <property name="ClientName" column="ClientName" />
    <loader query-ref="GetClients"/>
  </class>
  <sql-query name="GetClients" callable="true">
    <return class="Client" />
    call procedure MyPackage.GetClients(:int_SummitGroupId)
  </sql-query>
</hibernate-mapping>

这是我的单元测试:

        try
        {
            var cfg = new Configuration();
            cfg.Configure();
            cfg.AddAssembly( typeof( Client ).Assembly );

            ISessionFactory sessionFactory = cfg.BuildSessionFactory();
            IStatelessSession session = sessionFactory.OpenStatelessSession();

            IQuery query = session.GetNamedQuery( "GetClients" );
            query.SetParameter( "int_SummitGroupId", 3173 );
            IList<Client> clients = query.List<Client>();

            Assert.AreNotEqual( 0, clients.Count );
        }
        catch( Exception ex )
        {
            throw ex;
        }

我认为我可能引用了不正确的程序集,因为如果我将域模型对象放入MyComapny.MyProduct.Data.Oracle类中,它将起作用.只有当我分成两个项目时,我才会遇到这个问题.

I think I may be improperly referencing the assembly, because if I do put the domain model object in the MyComapny.MyProduct.Data.Oracle class it works. Only when I separate out in to two projects do I run into this problem.

推荐答案

是的,有可能.如果映射在程序集"MyCompany.MYProduct.Data.Oracle"上,则必须将THAT程序集传递给cfg.AddAssembly().您正在使用程序集"MyCompany.MyProduct.Core"

Yes, It's possible. If the mappings are on the assembly "MyCompany.MYProduct.Data.Oracle" then you have to pass THAT assembly to cfg.AddAssembly(). You're are using the assembly "MyCompany.MyProduct.Core"

cfg.AddAssembly("MyCompany.MYProduct.Data.Oracle");

这篇关于单独项目中的nHibernate域模型和映射文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 01:27