Fluent Nhibernate - 映射组件集合(值对象)?

发布于 2024-10-02 17:59:37 字数 604 浏览 2 评论 0原文

我目前正在使用这样的组件映射:

public class UserMapping
{
         public UserMapping()
         {
            Id(c => c.Id).GeneratedBy.HiLo("100");
            Map(c => c.UserName);
            Component(c => c.Country, CountryComponentMapping.Map);
         }
}


public sealed class CountryComponentMapping
{
    public static void Map(ComponentPart<Country> part)
    {
        part.Map(x => x.CountryName)
        part.Map(x => x.CountryAlpha2)
    }
}

我喜欢这个,因为我只需在一处定义组件/值对象的映射。

我将如何对组件集合使用相同的语义?(例如,假设我们想将其更改为用户实体上的国家/地区集合)

I am currently using component maps like this:

public class UserMapping
{
         public UserMapping()
         {
            Id(c => c.Id).GeneratedBy.HiLo("100");
            Map(c => c.UserName);
            Component(c => c.Country, CountryComponentMapping.Map);
         }
}


public sealed class CountryComponentMapping
{
    public static void Map(ComponentPart<Country> part)
    {
        part.Map(x => x.CountryName)
        part.Map(x => x.CountryAlpha2)
    }
}

I like this becuase I only have to define the mapping for the component/value object in one place.

How would I go about using the same semantics for a collection of the component? (e.g. lets assume we wanted to change this to a collection of countries on the user entity)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

泪意 2024-10-09 17:59:37

您可以将其映射为组件集合。不幸的是,Fluent NHibernate 中没有重载 HasMany().Component() 来允许您指定要使用 ComponentMap 的派生类。不过,您可以对上述技术进行修改。

public sealed class UserMap : ClassMap<User> {
    public UserMap() {
        Id(c => c.Id).GeneratedBy.HiLo("100");
        Map(x => x.Name);
        HasMany(x => x.Countries).Component(CountryComponentMapping.Map);
    }
}

public sealed class CountryComponentMapping {
    public static void Map(CompositeElementBuilder<Country> part) {
        part.Map(x => x.CountryName);
        part.Map(x => x.CountryAlpha2)
    }
}

You can map this as a Component Collection. Unfortunately there is no overload to HasMany().Component() in Fluent NHibernate that allows you to specify that you want to use a derived class of ComponentMap. You can use a modification of your technique above though.

public sealed class UserMap : ClassMap<User> {
    public UserMap() {
        Id(c => c.Id).GeneratedBy.HiLo("100");
        Map(x => x.Name);
        HasMany(x => x.Countries).Component(CountryComponentMapping.Map);
    }
}

public sealed class CountryComponentMapping {
    public static void Map(CompositeElementBuilder<Country> part) {
        part.Map(x => x.CountryName);
        part.Map(x => x.CountryAlpha2)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文