如何使用 FluentNHibernate 配置通用组件?
这是我要为其配置映射的组件
public class Range<T> : ValueObject
{
public virtual T Start {get; set;}
public virtual T Finish {get; set;}
}
在我的域中,我有许多具有 Range << 等属性的实体。日期时间>,范围<整数> ...对于具有属性 x 的特定类,我们以这种方式配置组件:
persistenceModel.FindMapping<Customer>()
.Component<Address> (
x => x.CustomerAddress,
m =>
{
m.Map(x => x.Street).WithLengthOf(100);
m.Map(x => x.PostalCode).WithLengthOf(6);
m.Map(x => x.Town).WithLengthOf(30);
m.Map(x => x.Country).WithLengthOf(50);
});
整个域作为通用 T 的约定看起来如何? 我是不是错过了什么。 FluentNhibernate 不可能吗?
Here is the component for which I want to configure mapping
public class Range<T> : ValueObject
{
public virtual T Start {get; set;}
public virtual T Finish {get; set;}
}
In my domain I have many entities which have properties like Range < DateTime>, Range < int > ... for a particular class with property x we configure the component this way:
persistenceModel.FindMapping<Customer>()
.Component<Address> (
x => x.CustomerAddress,
m =>
{
m.Map(x => x.Street).WithLengthOf(100);
m.Map(x => x.PostalCode).WithLengthOf(6);
m.Map(x => x.Town).WithLengthOf(30);
m.Map(x => x.Country).WithLengthOf(50);
});
How looks the convention for whole domain as generic T ?
Do I miss something. Is not possible with FluentNhibernate ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该将
FindMapping
用于此目的。能够通过该方法更改映射是一种疏忽,绝对不应该依赖。该方法用于检查持久性模型,而不是更改它。如果您使用自动映射,则应该查看覆盖。我相信您的问题可以通过
ICompositeUserType
实现来解决;网上有一些关于如何实现这些的资源,特别是 通用复合用户类型实现。您只需照常映射范围属性,但使用CustomType
为其提供用户类型。您还可以使用新引入的 ComponentMap 功能来实现此目的,但它不支持在不使用基类的情况下映射开放泛型类型。
像这样的东西可能会起作用:
诚然,这并不理想。
You should not be using
FindMapping
for that purpose. Being able to alter the mappings through that method is an oversight, and should definitely not be relied upon. That method is for inspecting the persistence model, not altering it. If you're using automapping, you should look into overrides.I believe your problem could be solved with an
ICompositeUserType
implementation; there are several resources available online on how to implement these, specifically a generic composite user type implementation. You'd just map your range property as normal, but supply the user type to it usingCustomType
.You could also do it with the newly introduced
ComponentMap
functionality, but it doesn't support mapping open generic types without the use of base-classes.Something like this would probably work:
Admittedly, it's not ideal.