在 NHibernate 中映射视图的策略 [英] Strategies for Mapping Views in NHibernate
问题描述
NHibernate 似乎需要将 id 标记指定为映射的一部分.这给视图带来了一个问题,因为大多数时候(根据我的经验)视图没有 Id.我之前在 nhibernate 中映射过视图,但是我这样做的方式对我来说似乎很混乱.
It seems that NHibernate needs to have an id tag specified as part of the mapping. This presents a problem for views as most of the time (in my experience) a view will not have an Id. I have mapped views before in nhibernate, but they way I did it seemed to be be messy to me.
这是一个人为的示例,说明我目前的做法.
Here is a contrived example of how I am doing it currently.
映射
<class name="ProductView" table="viewProduct" mutable="false" >
<id name="Id" type="Guid" >
<generator class="guid.comb" />
</id>
<property name="Name" />
<!-- more properties -->
</class>
查看 SQL
Select NewID() as Id, ProductName as Name, --More columns
From Product
课程
public class ProductView
{
public virtual Id {get; set;}
public virtual Name {get; set;}
}
我不需要产品的 ID,或者在某些视图的情况下,我可能没有视图的 ID,具体取决于我是否可以控制视图
I don't need an Id for the product or in the case of some views I may not have an id for the view, depending on if I have control over the View
在 nhibernate 中是否有更好的方法将视图映射到对象?
Is there a better way of mapping views to objects in nhibernate?
编辑
到目前为止的答案
映射
<class name="ProductView" table="viewProduct" mutable="false" >
<id name="Id" type="Guid" />
<property name="Name" />
<!-- more properties -->
</class>
班级
public class ProductView
{
public virtual Name {get; set;}
//more properties
}
查看 SQL
我还需要 NewID() 吗?
View SQL
Do I still need NewID()?
Select NewID() as Id, ProductName as Name, --More columns
From Product
推荐答案
您可以通过不将 Id 映射到属性并省略生成器来使其更简洁:
You can make it just a little bit cleaner by not mapping the Id to a property and omitting the generator:
<id column="Id" type="guid"/>
这样,您就可以将问题保留在数据层,而不会将实现细节泄露给您的域.
That way, you keep the problem in the data layer, without leaking the implementation detail to your domain.
这篇关于在 NHibernate 中映射视图的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!