为子对象数据映射 [英] Data Mapper for Child Objects
问题描述
假设我们有一个有一个名为应对复杂的属性Customer类。事情是这样的:
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; }
}
我需要实现数据映射模式坚持客户对象数据库。我应该是这样的CustomerDataMapper坚持客户和地址或2个不同的数据映射器:CustomerDataMapper和AddressDataMapper?
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.
干杯,
MOSH
推荐答案
如果你想手工图,我会建议有两个映射器,以 CustomerDataMapper
委托给 AddressDataMapper
该属性。这使您可以在其他地方分开,并可能测试映射重复使用的地址映射器。
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.
像LINQ的ORM to SQL或NHibernate的可以照顾这个给你。
An ORM like LINQ to SQL or NHibernate could take care of this for you.
这篇关于为子对象数据映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!