I have noticed that many people recommend creating a data mapper for each model but that can be time consuming. Why wouldn't it beneficial to create a generic data mapper that detects an object's(model) properties(variables) and matches them to a database field?
我注意到很多人建议为每个模型创建一个数据映射器,但这可能非常耗时。为什么创建检测对象(模型)属性(变量)并将它们与数据库字段匹配的通用数据映射器不是很有益?
1 个解决方案
#1
"Why wouldn't it beneficial to create a generic data mapper"
“为什么创建通用数据映射器不会有益”
There are absolutely benefits (e.g. less duplicate code), but there are some potential downsides if you're going to directly consume it. A couple of potential pitfalls (off the top of my head)...
绝对有益(例如,重复代码较少),但如果您要直接使用它,则存在一些潜在的缺点。一些潜在的陷阱(脱离我的头脑)......
Given this example: IUser user = DataMapper.Map(HashTable userRecord);
给出这个例子:IUser user = DataMapper.Map(HashTable userRecord);
- What happens if the userRecord contains invalid data? Should the Map method verify the HashTable contains all the pertinent information before attempting to fill the User object? Will that verification process vary by object type?
- What if the object is modified to take a constructor parameter?
如果userRecord包含无效数据会发生什么?在尝试填充User对象之前,Map方法是否应该验证HashTable是否包含所有相关信息?验证过程会因对象类型而异吗?
如果修改对象以获取构造函数参数该怎么办?
I've been down this road myself, and eventually wound up with a lot of objects being built using a generic mapper and other objects using non-generic mappers, and it ends up being very confusing for the developer (which mapper should I use for this object?), and a few bits and pieces of scary "cleverness" inside the mapping methods.
我自己一直走在这条路上,最终结束了许多使用通用映射器构建的对象和使用非泛型映射器的其他对象,并且最终导致开发人员感到非常困惑(我应该使用哪个映射器)这个对象?),以及映射方法中的一些可怕的“聪明”。
You can absolutely use a generic data mapper, but you may want to consider using it as an abstract base class and writing specialized mappers on top of it to avoid challenges with "special" objects, and to avoid a bunch of deep, potentially dangerous refactoring when an object needs to change in some fashion that the mapper won't support.
您绝对可以使用通用数据映射器,但您可能需要考虑将其用作抽象基类并在其上编写专门的映射器以避免“特殊”对象的挑战,并避免一堆深层的,潜在危险的重构当一个对象需要以某种方式改变时,映射器将不支持。
[Note that I do not consider myself (even remotely) an expert in the context of data/object mapping, so take anything I suggest with a grain of salt]
[请注意,我不认为自己(甚至是远程)是数据/对象映射环境中的专家,因此,我采取任何我建议的东西,以一粒盐]
#1
"Why wouldn't it beneficial to create a generic data mapper"
“为什么创建通用数据映射器不会有益”
There are absolutely benefits (e.g. less duplicate code), but there are some potential downsides if you're going to directly consume it. A couple of potential pitfalls (off the top of my head)...
绝对有益(例如,重复代码较少),但如果您要直接使用它,则存在一些潜在的缺点。一些潜在的陷阱(脱离我的头脑)......
Given this example: IUser user = DataMapper.Map(HashTable userRecord);
给出这个例子:IUser user = DataMapper.Map(HashTable userRecord);
- What happens if the userRecord contains invalid data? Should the Map method verify the HashTable contains all the pertinent information before attempting to fill the User object? Will that verification process vary by object type?
- What if the object is modified to take a constructor parameter?
如果userRecord包含无效数据会发生什么?在尝试填充User对象之前,Map方法是否应该验证HashTable是否包含所有相关信息?验证过程会因对象类型而异吗?
如果修改对象以获取构造函数参数该怎么办?
I've been down this road myself, and eventually wound up with a lot of objects being built using a generic mapper and other objects using non-generic mappers, and it ends up being very confusing for the developer (which mapper should I use for this object?), and a few bits and pieces of scary "cleverness" inside the mapping methods.
我自己一直走在这条路上,最终结束了许多使用通用映射器构建的对象和使用非泛型映射器的其他对象,并且最终导致开发人员感到非常困惑(我应该使用哪个映射器)这个对象?),以及映射方法中的一些可怕的“聪明”。
You can absolutely use a generic data mapper, but you may want to consider using it as an abstract base class and writing specialized mappers on top of it to avoid challenges with "special" objects, and to avoid a bunch of deep, potentially dangerous refactoring when an object needs to change in some fashion that the mapper won't support.
您绝对可以使用通用数据映射器,但您可能需要考虑将其用作抽象基类并在其上编写专门的映射器以避免“特殊”对象的挑战,并避免一堆深层的,潜在危险的重构当一个对象需要以某种方式改变时,映射器将不支持。
[Note that I do not consider myself (even remotely) an expert in the context of data/object mapping, so take anything I suggest with a grain of salt]
[请注意,我不认为自己(甚至是远程)是数据/对象映射环境中的专家,因此,我采取任何我建议的东西,以一粒盐]