对象/数据库映射属于哪里?
我目前正在开发一个使用轻量级 DataAccessLayer 的项目。该项目由业务层、数据访问层和保存 DTO 的项目组成。这个设置是固定的,我无法更改。我也无法使用像 Nhibernate 或实体框架这样成熟的 ORM。
好的,为了正确使用此设置,我们需要以某种方式将 DTO 的属性映射到数据库的字段。目前我们正在 dataaccesslayer 中执行此操作(每个实体/DTO“拥有”一个 dataaccesslayer 类)。 这发生在两个方法 MapTo 和 MapFrom 中:第一个将对象的字段映射到表的字段,另一个则执行相反的操作。
现在我想知道 dataaccesslayer 是否是定义此映射的正确位置。在 DTO 中定义映射不是更好吗?例如属性? 或者更好的是一个更通用的方法,而不是每个类的方法,它基本上只是将属性映射到表字段。
I'm currently working on a project where we are using a lightweight DataAccessLayer. The project consists of a businesslayer,a dataacesslayer and a project which holds the DTOs. This setup is fix and I cannot change. Neither can I use a full fledged ORM like Nhibernate or Entity Framework.
ok, to properly work with this setup we need to somehow map our DTO's properties to the fields of the database. Currently we are doing this in the dataaccesslayer (every entity/DTO "possesses" a dataaccesslayer-class).
This happens in two methods MapTo and MapFrom: the first maps the object's fields to the table's fields and the other does the reverse.
Now I'm wondering if the dataaccesslayer is the correct place to define this mapping. Wouldn't it be better to define the mapping in the DTO's. For example with attributes?
Or better a more generic method instead of a method for every class which basically just maps properties to table fields.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您指的是 DTO 的域模型,对吧?通常,实体和表之间的映射属于数据访问层。但我不会将映射放入域模型项目中。在这种情况下,例如,当您的数据库表发生更改时,您必须更改/编译域模型以及 DAL。在其他示例中,如果您在DAL中有映射,当您切换数据库或ORM方法时,只需更改DAL即可实现。
You mean domain model by DTO, right? Typically, the mapping between entities and tables belongs to data access layer. But I wouldn't put the mapping into domain model project. In that case, for example, when your DB table changes, then you have to change/compile domain model as well as DAL. In other example, if you have the mapping in DAL, when you switch database or ORM method, you can achieve that with only change of DAL.