问题
Assume we have a Customer class which has a complex property called Address. Something like this:
public class Customer
{
public string Name { get; set; }
public Address { get; set; }
}
I need to implement Data Mapper pattern to persist Customer objects to the database. Should I have something like CustomerDataMapper that persists the Customer AND the Address or 2 different Data Mappers: CustomerDataMapper and AddressDataMapper?
I'd like to know your thoughts on this.
Cheers,
Mosh
回答1:
If you want to map by hand, I would suggest having two mappers, with CustomerDataMapper
delegating to AddressDataMapper
for that property. This allows you to test the mappings separately and potentially reused the address mapper elsewhere.
An ORM like LINQ to SQL or NHibernate could take care of this for you.
来源:https://stackoverflow.com/questions/3402685/data-mapper-for-child-objects