2010-04-12 71 views
3

有沒有辦法將域對象和映射文件分離成兩個單獨的項目?我想創建一個名爲MyCompany.MyProduct.Core的項目,其中包含我的域模型,另一個名爲MyCompany.MYProduct.Data.Oracle的項目包含我的Oracle數據映射。但是,當我嘗試進行單元測試時,我收到以下錯誤消息:nHibernate域模型和映射文件在單獨的項目

未找到命名查詢'GetClients'。

這裏是我的映射文件:

<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類中的作用。只有當我分離出兩個項目時,我纔會遇到這個問題。

回答

5

是的,這是可能的。如果映射位於程序集「MyCompany.MYProduct.Data.Oracle」上,則必須將該程序集傳遞給cfg.AddAssembly()。你正在使用匯編 「MyCompany.MyProduct.Core」

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

+0

謝謝!那只是我需要的答案。 – 2010-04-12 14:41:21

+0

@Blake Blackwell:不客氣!樂意效勞 :-) – 2010-04-12 14:45:12