在与域对象不同的程序集中定义NHibernate映射 [英] define NHibernate mappings in different assembly than the domain objects
问题描述
我有一个程序集 Foo.Bar ,其中包含我的所有域逻辑和域对象(例如public class Project
,public interface IProjectRepository
).
I have an assembly Foo.Bar that holds all my domain logic and my domain object (e.g. public class Project
, public interface IProjectRepository
).
我还有一个程序集 Foo.Bar.Data ,它充当我的NHinbernate数据访问层,其中包含基于IProjectRepository
NHibernate的实现.它引用 Foo.Bar ,并包含诸如 Project.hbm.xml 之类的映射文件.
I also have an assembly Foo.Bar.Data that acts as my NHinbernate data access layer wich holds the IProjectRepository
NHibernate-based implementation. It references Foo.Bar and contains the mapping files such as Project.hbm.xml.
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Foo.Bar"
namespace="Foo.Bar.Domain" default-lazy="false">
<class name="Project">
<id name="Id" column="ID" />
<property name="Customer" column="CUSTOMER" />
</class>
</hibernate-mapping>
不幸的是,使用此设置,我得到以下异常:
Unfortunately with this setup I get the following exception:
NHibernate.MappingException:无持久性:Foo.Bar.Domain.Project
NHibernate日志显示:
And the NHibernate log says:
WARN NHibernate.Cfg.Configuration(空)-程序集中未找到映射的文档:Foo.Bar,版本= 1.0.0.0,区域性=中性,PublicKeyToken =空
但是,如果我将映射文件添加到 Foo.Bar ,则一切正常.但是我真的不希望任何数据访问或NHinbernate实现细节进入我的域程序集.这往往是最好的做法,对吗?这种设置不是一个好方法吗?
Howver if I add the mapping file to the Foo.Bar assmbly everything works fine. But I really do not want any data access or NHinbernate implementation details into my domain assembly. This tends to be best practise, right? Is this kind of setup not a good way to go?
如何让NHibernate在 Foo.Bar.Data *程序集中找到映射文件?
How can I make NHibernate find the mapping files in the Foo.Bar.Data* assembly?
推荐答案
原来,我要做的就是添加
Turns out that all I have to do is add
<mapping assembly="Foo.Bar.Data"/>
在<session-factory>
元素中的隐藏配置.
这篇关于在与域对象不同的程序集中定义NHibernate映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!