如何使 POCO 与具有 DDD 中复杂关系的 Linq-to-SQL 配合使用
当我的域模型不是表驱动时,我正在努力寻找一种使 POCO 与 Linq-to-Sql 一起使用的方法 - 这意味着我的域对象与数据库架构不匹配。
例如,在我的域层中,我有一个 Appointment 对象,它具有 Recurrence 类型的 Recurrence 属性。这是一个具有多个子类的基类,每个子类都基于特定的重复模式。
在我的数据库中,当约会记录与其重复之间始终存在一对一关系时,拥有单独的 AppointmentRecurrences 表是没有意义的。因此,Appointments 表具有 RecurrenceType 和 RecurrenceValue 列。 RecurrenceType 与 RecurrenceTypes 表具有外键关系,因为重复类型(模式)与 Appointments 表之间存在一对多关系。
除非有办法在 Linq-to-Sql 中创建这两个模型之间的正确映射,否则我只能手动解决代码中的阻抗不匹配问题。
当使用规范模式查询数据库时,这变得更加困难。例如,如果我想返回当前约会的列表,我可以轻松创建一个使用以下表达式的规范对象: appt => appt.Recurrence.IsDue
。但是,这不会转换为 Linq-to-SQL 空间,因为表达式的源类型不是 L2S 识别的类型(例如,它不是 L2S 实体)。
那么如何在 Linq-to-SQL 中创建复杂的映射来支持我的域模型?
或者,在这种情况下是否有更好的方法来实现规范模式?我曾考虑过使用由我的域对象和 L2S 实体(通过部分)实现的接口,但由于两个对象图的阻抗不匹配,这是不可能的。
建议?
I am struggling to find a way to make POCOs work with Linq-to-Sql when my domain model is not table-driven - meaning that my domain objects do not match-up with the database schema.
For example, in my domain layer I have an Appointment object which has a Recurrence property of type Recurrence. This is a base class with several subclasses each based on a specific recurrence pattern.
In my database, it makes no sense to have a separate AppointmentRecurrences table when there is always a one-to-one relationship between the Appointment record and its recurrence. So, the Appointments table has RecurrenceType and RecurrenceValue columns. RecurrenceType has a foreign key relationship to the RecurrenceTypes table because there is a one-to-many relationship between the recurrence type (pattern) and the Appointments table.
Unless there is a way to create the proper mapping between these two models in Linq-to-Sql, I am left with manually resolving the impedence mismatch in code.
This becomes even more difficult when it comes to querying the database using the Specification pattern. For example, if I want to return a list of current appointments, I can easily create a Specification object that uses the following Expression: appt => appt.Recurrence.IsDue
. However, this does not translate into the Linq-to-SQL space because the source type of the Expression is not one that L2S recognizes (e.g. it's not the L2S entity).
So how can I create the complex mapping in Linq-to-SQL to support my domain model?
Or, is there a better way to implement the Specification pattern in this case? I'd thought about using interfaces that would be implemented by both my domain object and the L2S entity (through partials) but that's not possible with the impedence mismatch of the two object graphs.
Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,Linq to SQL 几乎迫使您采用每表类模型,它不支持将单个实体类映射到多个数据库表。
更不幸的是,很少有 ORM 能够支持更复杂的映射,而且很少有能够支持并提供像样的 LINQ 支持。我唯一可以肯定的是 NHibernate(我们使用实体框架的经验认为它在这方面并不比 L2S 更好)。
此外,尝试在 LINQ 表达式中使用规范模式将是一个相当大的挑战。
即使使用 ORM,甚至使用 NHibernate 等真正强大的抽象 ORM,仍然需要克服很大的阻抗不匹配。
Unfortunately, Linq to SQL pretty much forces you into a class-per-table model, it does not support mapping a single entity class to several database tables.
Even more unfortunately, there are very few ORM's that will support more complicated mappings, and vanishingly few that do and offer decent LINQ support. The only I'm even remotely sure of is NHibernate (our experiences with Entity Framework rate it really no better than L2S in this regard).
Also, trying to use the specification pattern in LINQ expressions is going to be quite the challenge.
Even with ORM's, and even with a really strong abstracting ORM like NHibernate, there is still a large impedence mismatch to overcome.
此帖子 解释如何将规范模式与 linq-to-sql 结合使用。这些规范可以链接在一起,构建一个表达式树,可供您的存储库使用,因此也可用于 linq-to-sql。
我还没有尝试实现它,但 linq-to-entities 版本已在我当前正在进行的项目的待办事项列表中。
This post explains how to use the specification pattern with linq-to-sql. The specifications can be chained together which builds up an expression tree that can be used by your repository and therefore linq-to-sql.
I haven't tried implementing it yet, but the linq-to-entities version is on my to-do list for a project I am currently working on.