How to get POCO to work with Linq-to-SQL with complex relationships in DDD

I am trying to find a way to get POCOs to work with Linq-to-Sql when my domain model is not controlled by a table - this means that my domain objects do not match the database schema.

For example, at my domain level, I have an Appointment object that has a Recurrence property of type Recurrence. This is a base class with several subclasses, each based on a specific repetition 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 repetition. Thus, the Assignments table contains the columns RecurrenceType and RecurrenceValue. RecurrenceType has a foreign key relationship to the RecurrenceTypes table because there is a one-to-many relationship between the repeat type (pattern) and the Destination table.

If there is a way to create the correct mapping between the two models in Linq-to-Sql, I remain with the manual resolution of the impedance mismatch in the code.

This gets even more complicated when it comes to querying the database using the Specification template. For example, if I want to return a list of current assignments, I can easily create a Specification object that uses the following expression: appt => appt.Recurrence.IsDue . However, this does not translate into Linq-to-SQL space, because the original expression type is not the one that recognizes L2S (for example, it is not an L2S object).

So, how can I create complex mapping in Linq-to-SQL to support my domain model?

Or is there a better way to implement a specification template in this case? I was thinking about using interfaces that will be implemented by both my domain object and the L2S object (through partial ones), but this is impossible with the impedance mismatch between the two object graphs.

Suggestions?

+4
source share
2 answers

Unfortunately, Linq to SQL pretty much forces you to use a class model for a table; it does not support mapping a single entity class to multiple database tables.

Unfortunately, there is still very little ORM that will support more complex comparisons, and, as a rule, few will make them and offer decent LINQ support. The only thing I even remotely know is NHibernate (our experience with the Entity Framework in this regard is no better than L2S).

In addition, trying to use the specification template in LINQ expressions will be quite a challenge.

Even with ORM and even with a really strong abstraction ORM like NHibernate, there is still a large impedance mismatch to overcome.

+3
source

This post explains how to use the specification template using linq-to-sql. Specifications can be combined together, which creates an expression tree that can be used by your repository, and therefore linq-to-sql.

I have not tried to implement it yet, but the linq-to-entity version is on my to-do list for the project I'm currently working on.

+1
source

Source: https://habr.com/ru/post/1347293/


All Articles