English 中文(简体)
Is using a table inheritance a valid way to avoid using a join table?
原标题:

I ve been following a mostly DDD methodology for this project, so, like any DDD er, I created my domain model classes first. My intention is to use these POCO s as my LINQ-to-SQL entities (yes, they re not pure POCO s, but I m ok with that). I ve started creating the database schema and external mapping XML file, but I m running into some issues with modeling the entities relationships and associations.

An artifact represents a document. Artifacts can be associated with either a Task or a Case. The Case entity looks like this:

public class Case 
{
     private EntitySet<Artifact> _Artifacts;
     public IList<Artifact> Artifacts
     {
          get
          {
               return _Artifacts;
          }
          set
          {
               _Artifacts.Assign(value);
          }
     }
     .
     .
     .
}

Since an Artifact can be associated with either a Case, or a Task, I ve the option to use inheritance on the Artifact class to create CaseArtifact and TaskArtifact derived classes. The only difference between the two classes, however, would be the presence of a Case field or a Task field. In the database of course, I would have a single table, Artifact, with a type discriminator field and the CaseId and TaskId fields.

My question: is this a valid approach to solving this problem, or would creating a join table for each association (2 new tables, total) be a better approach?

最佳回答

I would probably go with two tables - it makes the referential integrity-PK/FKs a little simpler to handle in the database, since you won t have to have a complex constraint based on the selector column.

(to reply to your comment - I ran out of space so post here as an edit) My overall philosophy is that the database should be modelled with database best practices (protect your perimeter and ensure database consistency, using as much RI and constraints as possible, provide all access through SPs, log activity as necessary, control all modes of access, use triggers where necessary) and the object model should be modelled with OOP best practices to provide a powerful and consistent API. It s the job of your SPs/data-access layer to handle the impedance mismatch.

If you just persist a well-designed object model to a database, your database won t have much intrinsic value (difficult to data mine, report, warehouse, metadata vague, etc) when viewed without going through the lens of the object model - this is fine for some application, typically not for mine.

If you just mimic a well-designed database structure in your application, without providing a rich OO API, your application will be difficult to maintain and the internal strucutres will be awkward to deal with - typically very procedural, rigid and with a lot of code duplication.

问题回答

I would consider finding commonalities in between case and task, for the lack of better word let s call it "CaseTask" and then sub-typing (inheriting) from that one. After that you attach document to the super-type.

UPDATE (after comment):
I would then consider something like this. Each document can be attached to several cases or tasks.



artifact_model_01D





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签