这是我要配置映射的组件
public class Range<T> : ValueObject
{
public virtual T Start {get; set;}
public virtual T Finish {get; set;}
}
在我的域中,我有许多具有 Range < DateTime>、Range < int > 等属性的实体...对于具有属性 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 不可能吗?
您不应该将
FindMapping
用于此目的。能够通过该方法更改映射是一种疏忽,绝对不应该依赖。该方法用于“检查”持久性模型,而不是更改它。如果您使用自动映射,您应该查看 overrides。
我相信您的问题可以通过 ICompositeUserType
实施来解决;网上有一些关于如何实现这些的资源,特别是
通用复合用户类型实现。您只需照常映射范围属性,但使用
CustomType
为其提供用户类型。Map(x => x.Range)
.CustomType<RangeUserType>();
您也可以使用新引入的
ComponentMap
功能来实现此目的,但它不支持在不使用基类的情况下映射
open泛型类型。 这样的东西可能会起作用:
public abstract class RangeMap<T> : ComponentMap<T>
{
protected RangeMap()
{
Map(x => x.Start);
Map(x => x.Finish):
}
}
public class IntRangeMap : RangeMap<int>
{}
public class DateRangeMap : RangeMap<DateTime>
{}
诚然,这并不理想。