Applying Domain Model on top of Linq2Sql entities

Posted by Thomas on Stack Overflow See other posts from Stack Overflow or by Thomas
Published on 2010-04-29T02:49:15Z Indexed on 2010/04/29 2:57 UTC
Read the original article Hit count: 392

Filed under:
|

I am trying to practice the model first approach and I am putting together a domain model. My requirement is pretty simple: UserSession can have multiple ShoppingCartItems.

I should start off by saying that I am going to apply the domain model interfaces to Linq2Sql generated entities (using partial classes). My requirement translates into three database tables (UserSession, Product, ShoppingCartItem where ProductId and UserSessionId are foreign keys in the ShoppingCartItem table). Linq2Sql generates these entities for me. I know I shouldn't even be dealing with the database at this point but I think it is important to mention.

The aggregate root is UserSession as a ShoppingCartItem can not exist without a UserSession but I am unclear on the rest. What about Product? It is defiently an entity but should it be associated to ShoppingCartItem?

Here are a few suggestion (they might all be incorrect implementations):

public interface IUserSession {
    public Guid Id { get; set; }
    public IList<IShoppingCartItem> ShoppingCartItems{ get; set; }
}

public interface IShoppingCartItem {
    public Guid UserSessionId { get; set; }
    public int ProductId { get; set; }        
}

Another one would be:

public interface IUserSession {
    public Guid Id { get; set; }
    public IList<IShoppingCartItem> ShoppingCartItems{ get; set; }
}

public interface IShoppingCartItem {
    public Guid UserSessionId { get; set; }
    public IProduct Product { get; set; }         
}

A third one is:

public interface IUserSession {
    public Guid Id { get; set; }
    public IList<IShoppingCartItemColletion> ShoppingCartItems{ get; set; }
}

public interface IShoppingCartItemColletion {
    public IUserSession UserSession { get; set; }
    public IProduct Product { get; set; }      
}

public interface IProduct {
    public int ProductId { get; set; }      
}

I have a feeling my mind is too tightly coupled with database models and tables which is making this hard to grasp. Anyone care to decouple?

© Stack Overflow or respective owner

Related posts about ddd

Related posts about linq-to-sql