当属性和支持字段没有任何共同点时的自动映射约定?
使用 Fluent NHibernate,我似乎无法为以下(看似简单且常见)用例设计必要的自动映射约定:
public class MyClass
{
private int _specialIdentityField
private string _firstname;
public Id { get { return _specialIdentityField; }; }
public virtual string Firstname
{
get
{
return _firstname;
}
set
{
_firstname = value;
}
}
}
public class OtherClass
{
private int _specialIdentityField
private string _lastname;
public Id { get { return _specialIdentityField; }; }
public virtual string Lastname
{
get
{
return _lastname;
}
set
{
_lastname = value;
}
}
}
所需的映射如下所示:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="field.camelcase-underscore" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="MyClass" table="`MyClass`">
<id name="_specialIdentityField" type="System.Int32" access=field>
<column name="Id" />
<generator class="identity" />
</id>
<property name="Firstname" type="System.String">
<column name="Firstname" />
</property>
</class>
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="OtherClass" table="`OtherClass`">
<id name="_specialIdentityField" type="System.Int32" access=field>
<column name="Id" />
<generator class="identity" />
</id>
<property name="Lastname" type="System.String">
<column name="Lastname" />
</property>
</class>
</hibernate-mapping>
基本上规则是:
- 一切都是 field-camelcase-underscore 作为访问类型,除了身份
- 身份是每个类中的固定名称字段 (name=_someSpecialIdentityField)
- 身份访问始终仅限于字段,并且与没有关系它周围的 RO 属性的名称
完全让我困惑的部分是身份相关元素的约定映射(显然,属性的约定映射完全是标准费用)。我遇到的问题是如何告诉 FNH 约定我的身份字段是固定名称。我能找到的所有约定覆盖似乎都假设表示身份的属性与其底层支持字段的名称之间始终存在某种关系(例如,我可以为支持字段,但似乎不明白我如何可以说“这始终是支持字段的名称”)。
对我来说,如何通过显式映射(并且就此而言,使用 XML 映射文件)来完成此任务是显而易见的,但对我来说,如何通过 FNH 中基于约定(自动映射)映射来完成此任务则一点也不明显。
这不可能是一个非典型的用例,所以我一定是忽略了一些非常明显的东西。感谢任何 FNH 专家的想法!
Using Fluent NHibernate, I cannot seem to devise the necessary automapping conventions for the following (seemingly simple and common) use-case:
public class MyClass
{
private int _specialIdentityField
private string _firstname;
public Id { get { return _specialIdentityField; }; }
public virtual string Firstname
{
get
{
return _firstname;
}
set
{
_firstname = value;
}
}
}
public class OtherClass
{
private int _specialIdentityField
private string _lastname;
public Id { get { return _specialIdentityField; }; }
public virtual string Lastname
{
get
{
return _lastname;
}
set
{
_lastname = value;
}
}
}
The desired mappings are like so:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="field.camelcase-underscore" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="MyClass" table="`MyClass`">
<id name="_specialIdentityField" type="System.Int32" access=field>
<column name="Id" />
<generator class="identity" />
</id>
<property name="Firstname" type="System.String">
<column name="Firstname" />
</property>
</class>
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="OtherClass" table="`OtherClass`">
<id name="_specialIdentityField" type="System.Int32" access=field>
<column name="Id" />
<generator class="identity" />
</id>
<property name="Lastname" type="System.String">
<column name="Lastname" />
</property>
</class>
</hibernate-mapping>
Basically the rules are:
- everything is field-camelcase-underscore as an access type EXCEPT identity
- identity is a fixed-name field in every class (name=_someSpecialIdentityField)
- identity access is always field-only and bears no relation to the name of the RO property that surrounds it
The part of this that is completely tripping me up is the convention-mapping of the identity-related elements (clearly the convention-mapping of the properties is completely standard fare). The issue I am having is how to tell FNH conventions that my identity field is a fixed-name. All of the convention overrides that I can find seem to assume there will always be some relationship between the property that represents identity and the name of its underlying backing field (e.g. I can set a 'custom prefix' for the backing field, but cannot seem to see how I can just say "this is always the name of the backing field").
Its obvious to me how to accomplish this with explicit mapping (and for that matter, with XML mapping files) but not obvious at all to me how to accomplish this with convention-based (Automapping) mapping in FNH.
This can't be an atypical use-case so I must just be overlooking something terribly obvious. Thoughts from any FNH gurus appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:看看 IAutomappingConfiguration 接口。创建您自己的实现或覆盖 DefaultAutomappingConfiguration 类。
然后将其添加到初始化中:
============================================ =====
嗨,史蒂夫,我想我知道你想做什么。我使用 Proteus 和 FNH 自动映射。为了用 id 来实现这一点,我在 Proteus 周围创建了一个包装器,它做了两件事:
1)映射 ID
2)隐藏 id 的 Setter
并避免属性 IsTransient 被持久化+您可以创建 MappingAlternation 的其他内容:
PS:我真的很怀念你活跃在网络空间的时光。两年前的夏天和秋天是令人兴奋的......
EDIT: Take a look at the IAutomappingConfiguration interface. Create you own implementation or override the DefaultAutomappingConfiguration class.
then you add it to the initialization:
===============================================
Hi Steve, I think I know what you are trying to do. I use Proteus and FNH automapping. To do the trick with the id I created a wrapper around Proteus which do two things:
1) Maps the ID
2) Hides the Setter for the id
And to avoid the property IsTransient to be persisted + other stuff you can create MappingAlternation:
PS: I am really missing the times when you were active in the online space. It was exciting Summer and Authumn 2 years ago...