views:

145

answers:

1

I'm trying to model my domain model using the entity framework. An example is Page class containing a Content class.

public class Page
{
   public virtual int Id { get; set; }
   public virtual string Name { get; set; }
   public virtual Content PageContent { get; set; }
}

public class Content 
{
   public IList<Version> Versions { get; private set; }


   public Version GetLatestPublishedVersion()
   {
       //Some biz logic to get the latest published version
   }


   public Version GetLatestDraftVersion()
   {
     //Some biz logic to get the latest draft version
   }

   public void AddVersion() {}

   public void DeleteVersion() {}
  ....

}

The database model does not have a table for Content, in fact the table relationship is:

Pages Table
----------
Id
Name


Versions Table
--------------
Id
PageId FK PAGES.ID
Title
Body

How can i model the Content class in the conceptual model? I tried using a complex type, but it only hold scalar properties. Tried using an Entity type but i get a message which basically says set up a table in the database for the Content class, why?

I dont feel that the Domain Model is wrong, it was designed like this so the publishing concerns are not on the Page class. Typical encapsulation at work here.

Has anyone else bumped into this problem?

A: 

Your (conceptual) entity should fairly closely match the DB schema, and then you project onto your Content class:

var q = from p in Context.PageEntities  // PageEntity is different than Page
        where p.id == id
        select new Page
        {
            Id = p.Id,
            Name = p.Name,
            Content = new Content 
                      {
                          Versions = from v in p.Versions
                                     select new Version
                                     {
                                         Id = v.Id,
                                         Body = v.Body, // etc.
                                     }
                       }
         };

This lets your data model and publishing model evolve independently. You need only change the projection.

Craig Stuntz
@Craig: Why does the the conceptual entity need to closely match the DB schema? The point of separate conceptual and physical models is that the physical model can be very different to meet performance and space needs of the underlying store.
Eric J.
It's not so much that it *must* match the DB schema as it is that it should be more closely aligned with the DB schema than the presentation model. It's OK if your EF model depends on your DB schema. It's not OK if your EF model depends on your web views.
Craig Stuntz