问题
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.
回答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.
来源:https://stackoverflow.com/questions/5254464/ddd-mapping-value-objects-with-fluent-nhibernate-in-separate-tables