0

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.

Community
  • 1
  • 1
gdp
  • 8,032
  • 10
  • 42
  • 63

1 Answers1

1

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.

Community
  • 1
  • 1
Mikael Östberg
  • 16,982
  • 6
  • 61
  • 79
  • Hi mike, thanks for your input. Your example shows auto mapping of an Entity which will have an identity in the domain model. I am more curious about mapping a value object with only a persistent identity into a separate table and whether using the classmap for the value object is the correct way or not. – gdp Mar 10 '11 at 13:54
  • Of course. I read half of what you had written. Well, I guess what you have done looks ok to me. Ask the question at http://groups.google.com/group/dddcqrs to get some depth. Many of the big guys hang out there. – Mikael Östberg Mar 10 '11 at 16:17
  • East i will give you the answer as it has pushed me in the right direction. – gdp Mar 22 '11 at 09:53