如何使用 FluentNHibernate 配置通用组件?

发布于 2024-09-02 03:33:31 字数 829 浏览 2 评论 0原文

这是我要为其配置映射的组件

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事未完 2024-09-09 03:33:32

您不应该将 FindMapping 用于此目的。能够通过该方法更改映射是一种疏忽,绝对不应该依赖。该方法用于检查持久性模型,而不是更改它。如果您使用自动映射,则应该查看覆盖


我相信您的问题可以通过 ICompositeUserType 实现来解决;网上有一些关于如何实现这些的资源,特别是 通用复合用户类型实现。您只需照常映射范围属性,但使用 CustomType 为其提供用户类型。

Map(x => x.Range)
  .CustomType<RangeUserType>();

您还可以使用新引入的 ComponentMap 功能来实现此目的,但它不支持在不使用基类的情况下映射开放泛型类型。

像这样的东西可能会起作用:

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>
{}

诚然,这并不理想。

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 using CustomType.

Map(x => x.Range)
  .CustomType<RangeUserType>();

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:

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>
{}

Admittedly, it's not ideal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文