DDD - 使用Fluent nHibernate将值对象映射到不同的表中 [英] DDD - Mapping Value Objects with Fluent nHibernate in separate tables

查看:280
本文介绍了DDD - 使用Fluent nHibernate将值对象映射到不同的表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:



尝试编辑来得到这个问题的答案。为了尝试改善这个问题,下面是一个简明扼要的简要版本:

当将值对象映射到使用流利的单独表时,代码是如何去的nhibernate,还是有其他的吗?








<对于这个问题的目的,我使用流利的配置nhibernate。

我正在稳定地学习DDD,但是经过了一些关于值对象映射的说明。似乎有很多关于映射值对象作为组件的信息。不过,我想在一些情况下规范我的数据库,因此会给值对象一个持久性标识(如果我是正确的,不违反DDD值对象规则)。

我已经在SO上看过这个问题,但是想有关如何设置和映射实际值对象的信息。



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



我的查询是在映射一个值对象的时候,我想把它放在一个单独的表中。用下面的classmap映射值对象是最好的方法吗?

 公共类地址
{
保护虚拟int ID {get;}
公共虚拟字符串firstLine {get;}
公共虚拟字符串city {get;}
公共虚拟字符串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);




$ b $ p

$我建议你应该去Fluent NHibernate的自动映射功能,就像在Sharp Architecture项目中一样。

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

举个例子,从 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);






$ b你可以看到,他们只指定了Id属性并映射到Order,让所有其他属性按照约定映射。事实上,即使是Id也可以使用约定来映射。

真正好用的功能是您可以从您的域中生成数据库模式。这使得您可以使用SQLite进行集成测试。

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


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.

解决方案

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.

这篇关于DDD - 使用Fluent nHibernate将值对象映射到不同的表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆