如何使POCO与具有DDD中复杂关系的Linq-to-SQL一起使用 [英] How to make POCO work with Linq-to-SQL with complex relationships in DDD

查看:85
本文介绍了如何使POCO与具有DDD中复杂关系的Linq-to-SQL一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的域模型不是由表驱动时,我正在努力寻找使POCO与Linq-to-Sql兼容的方法-这意味着我的域对象与数据库架构不匹配.

例如,在我的域层中,我有一个约会对象,该对象的Recurrence属性为Recurrence类型.这是一个基类,具有多个子类,每个子类均基于特定的重复模式.

在我的数据库中,当约会记录与其重复之间始终存在一对一的关系时,没有单独的AppointmentRecurrences表是没有意义的.因此,约会表具有RecurrenceType和RecurrenceValue列. RecurrenceType与RecurrenceTypes表具有外键关系,因为在递归类型(模式)与约会表之间存在一对多的关系.

除非有一种方法可以在Linq-to-Sql中在这两个模型之间创建正确的映射,否则我将不得不手动解决代码中的阻抗不匹配.

在使用规范模式查询数据库时,这变得更加困难.例如,如果要返回当前约会的列表,则可以轻松创建使用以下表达式的Specification对象:appt => appt.Recurrence.IsDue.但是,这不会转换为Linq-to-SQL空间,因为Expression的源类型不是L2S可以识别的类型(例如,它不是L2S实体).

那么如何在Linq-to-SQL中创建复杂的映射以支持我的域模型?

或者,在这种情况下,是否有更好的方法来实现规范"模式?我曾考虑过使用将由我的域对象和L2S实体(通过局部对象)实现的接口,但是由于两个对象图的阻抗不匹配,这是不可能的.

建议?

解决方案

不幸的是,Linq to SQL几乎迫使您进入每表类模型,它不支持将单个实体类映射到多个数据库表. /p>

更不幸的是,很少有支持更复杂映射的ORM,也很少有能提供不错的LINQ支持的ORM.我什至唯一可以肯定的是NHibernate(我们在实体框架方面的经验在这方面确实不比L2S好.

此外,尝试在LINQ表达式中使用规范模式将是很大的挑战.

即使使用ORM,甚至使用像NHibernate这样的真正强大的抽象ORM,仍然需要克服很大的阻抗不匹配.

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?

解决方案

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.

这篇关于如何使POCO与具有DDD中复杂关系的Linq-to-SQL一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆