DDD - 使用 Fluent nHibernate 在单独的表中映射值对象

发布于 2024-10-20 21:47:05 字数 990 浏览 4 评论 0原文

编辑:

嗨,尝试编辑来回答这个问题。为了尝试改善这个问题,这里有一个直截了当的精简版本:

下面的代码是使用流畅的nhibernate将值对象映射到单独的表时要走的路,还是有其他选择?


您好,

出于这个问题的目的,我正在使用流畅配置的 nhibernate。

我正在稳步学习 DDD,但在对值对象的映射进行一些澄清之后。似乎有很多关于将值对象映射为组件的信息。然而,我想在某些情况下规范化我的数据库,因此会给值对象一个持久性标识(如果我是正确的,则不会违反 DDD 值对象规则)。

我已经看到这个问题,但想要更多有关如何设置和映射实际值对象的信息。

当将值对象映射到代表实体的表时,我很舒服。例如,将地址值对象作为组件映射到客户表中。

我的查询在于映射我想放置在单独的表中的值对象时。使用类映射映射值对象的最佳方法是像下面这样吗?我打算忽略 Id,它纯粹是为了实现休眠持久化。

public class Address
{
  protected virtual int id {get;}
  public virtual string firstLine {get;}
  public virtual string city {get;}
  public virtual string postcode {get;}
}

public class AddressMap : ClassMap<Address>
{
  public AddressMap()
  {
    Id(x => x.Id);
    Map(x=> x.firstline);
    Map(x=> x.city);
    Map(x=> x.postcode);
  }
}

提前致谢。

EDIT:

Hi, trying an edit to get this question answered. In order to try improve the question, here is a straight to the point condensed version:

Is the code below the way to go when mapping value objects to separate tables using fluent nhibernate, or is there an alternative?


Hi,

For the purpose of this question I am using nhibernate fluently configured.

I'm steadily learning DDD but am after some clarification with the mapping of value objects. There seems to be a lot of information regarding mapping value objects as components. However I would like to normalise my database in some instances therefore would give the value object a persistence identity (which if I'm correct, doesn't violate DDD value object rules).

I have seen this question on SO, but would like a bit more info on how to setup and map the actual value object.

I am comfortable when mapping a value object to a table which represents the entity. For example mapping an address value object into the customer table as a component.

My query lies in when mapping a value object which i want to place in a separate table. Is the best way to map the value object using classmap like below? I am planing to ignore the Id it is purely there for nhibernate persistence.

public class Address
{
  protected virtual int id {get;}
  public virtual string firstLine {get;}
  public virtual string city {get;}
  public virtual string postcode {get;}
}

public class AddressMap : ClassMap<Address>
{
  public AddressMap()
  {
    Id(x => x.Id);
    Map(x=> x.firstline);
    Map(x=> x.city);
    Map(x=> x.postcode);
  }
}

Thanks in advance.

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

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

发布评论

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

评论(1

自此以后,行同陌路 2024-10-27 21:47:05

我的建议是,您应该使用 Fluent NHibernate 的自动映射功能,就像它们在 Sharp Architecture 项目中所做的那样。

我已经在几个项目中使用过,它使您能够更多地关注领域,而不必太担心持久性。

作为示例,取自此处

public class CustomerMap : IAutoMappingOverride<Customer>
{
    public void Override(AutoMapping<Customer> mapping) {
        mapping.Not.LazyLoad();
        mapping.Id(x => x.Id, "CustomerID")
            .GeneratedBy.Assigned();

        mapping.HasMany(hm => hm.Orders).KeyColumn("CustomerID");
    }
}

如您所见,它们仅指定 Id 属性和到 Order 的映射,并让所有其他属性按照约定进行映射。事实上,甚至 Id 也可以使用约定进行映射。

真正伟大的功能是您可以从您的域生成数据库模式。例如,这使您能够使用 SQLite 进行集成测试。

看看吧。无论如何,它对我来说非常有用。

My suggestion is that you should go towards Fluent NHibernate's auto mapping features, like they do in the Sharp Architecture project.

I've used in several projects and it makes you able to focus on the domain more and not worry about the persistence that much.

As an example, taken from here:

public class CustomerMap : IAutoMappingOverride<Customer>
{
    public void Override(AutoMapping<Customer> mapping) {
        mapping.Not.LazyLoad();
        mapping.Id(x => x.Id, "CustomerID")
            .GeneratedBy.Assigned();

        mapping.HasMany(hm => hm.Orders).KeyColumn("CustomerID");
    }
}

As you can see, they only specify the Id property and the mapping to Order and let all other properties get mapped by convention. In fact, even the Id could be mapped using conventions.

The really great feature with this is that you can generate the database schema from your domain. This makes you able to do integration tests using SQLite for example.

Have a look at it. It has been very useful for me anyway.

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