views:

32

answers:

2

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?

+1  A: 

Looks like you are on the right track. Half of the whole "doing DDD right" is having the right base classes. Have a look at this great DDD applied to C# resource:

http://dddpds.codeplex.com/

The source code is available and is very readable.

So, with regards to having ID in the model. The ID is a database thing and the usual approach is to keep all persistence out of the Model and restrict the model to the business logic. However, one normally makes an exception for the identifier and buries it in the Model base class like so:

public class ModelBase {
  protected readonly object m_Key;
  public ModelBase(object key) {
    m_Key = key;
  }
}

This key is used by your persistence layer to talk to the database and is opaque. It's considered quite OK to downcast the key to the required type, because you know what it is.

Also, the Domain Objects are pretty much on the bottom of your architecture stack (just above the Infrastructure layer). This means that you can make them concrete classes. You will not have multiple implementations of the domain models, so the interfaces are unnecessary, which is what Domain Driven Design is about - Domain first.

public Class UserSession : ModelBase {
  public UserSession(Guid Id):base(Id) {}

  public Guid Id { get{ return m_Key as Guid;} }
  public IList<ShoppingCartItem> ShoppingCartItems{ get; set; }
}

public class ShoppingCartItem : ModelBase {
  public ShoppingCartItem ():base(null) {}

  public UserSession UserSession { get; set; }
  public Product Product { get; set; }         
}
Igor Zevaka
Igor, what about if I wanted to add Quantity to the ShoppingCartItem entity? Would I just add in public int Quantity {get; set;} to it?
Thomas
Yep. That strikes me as a domain property, so it belongs on the model.
Igor Zevaka
+1  A: 

Typical shopping cart or customer-order examples prefer making UserSession (or Order) the root of aggregate. Individual items should be children of this session/order. It is up you whether individual items in the cart should have a meaningful id. I would prefer no, since 5 widgets in the cart are indistinguishable from another 5 widgets. Hence, I would model cart items as a collection of value objects.

Common problem with shopping cart items is whether they should include price, or not. if you include price, you will have your cart independent from changes of product price. It is very desirable if you want to store you cart for historical reasons since it is valuable to know how much items in the cart cost according to price when they were bought, not according to current.

Product should form definitively an aggregate by itself. Period.

Now, I don't know if all of this is easily implementable in LINQ to SQL, but you can try.

Szymon Pobiega