Composite pattern and Csla

Composite pattern and Csla

Old forum URL: forums.lhotka.net/forums/t/4177.aspx


ajj3085 posted on Tuesday, January 15, 2008

Hi,

I'm currently trying to implement the Composite pattern with Csla.  I have a LineItem, which may (or may not) support containing other line items.  The implementation so far is very similar to that of the sample in Head First Design Patterns, for those with that book. 

The part I've come up against so far is tracking deleted items,because even the top level class which is deisgned to always be the root ends up inheriting from BusinessBase.  

There are behaviors from both BusinessBase and BusinessListBase that would seem to apply, but since we can't have multiple inheritence, I'm not sure what do to.  So far I've added my own deletion tracking to LineItemNode (the subclass of LineItem that supports containing children), but I'm not sure if this is the proper way to do this.

Has anyone else implemented the composite pattern around Csla objects?  Any insight?

RockfordLhotka replied on Thursday, January 17, 2008

Andy,

Your problem isn't clear to me from your description. Maybe that's because I haven't read the head first book?

ajj3085 replied on Thursday, January 17, 2008

My design is like this:

public abstract class LineItemContainer : BusinessBase<LineItemContainer> {
   // item properties / methods, most have default behaviors because most line items support these
   // regardless of whether or not they have child line items.
   public virtual decimal UnitPrice { get; set; }
   public virtual bool SupportsChildren { get; }
   public void MoveUp();
   // other line item stuff
 
   // collection stuff
   public virtual Add( LineItemContainer child );
   public virtual Remove( LineItemContainer child );
   // other collection stuff; all implementation here also throws NotSupportedExceptions
}

public abstract class LineItemLeaf : LineItemContainer {
   // item properties / methods, all implementation in this class throws NotSupportedException
   public override decimal UnitPrice { get; set; }
   public override bool SupportsChildren { get; }
   // other line item stuff

   // Methods like Add aren't overridden, so they still throw NotSupportedException
}

public abstract class LineItemNode : LineItemContainer {
   // possibly overrides default Line item behaviors

   // collection stuff
   public override Add( LineItemContainer child );
   public override Remove( LineItemContainer child );
   // other collection stuff
}

For the subclasses of LineItemNode, the instance should support both BusinessBase behaviors as well as BusinessListBase behaviors.   For example, the GroupLineItem can have a valid UnitPrice, Quantity can be set for the group, but it also contains directly other line items (they should hold anything that subclasses LineItemContainer, so leaves or other nodes).

So LineItemNodes need to track deleted children just like a BusinessListBase list would, but I'm not sure how to proceed, because LineItemNode already tracks back to BusinessBase.  This is the first time I wish C# had multiple inheritance.

RockfordLhotka replied on Thursday, January 17, 2008

I think the issue may be that CSLA isn’t designed to allow collections to have properties. There is no concept of a BB/BLB combination. You may need to create such a beast – merging much of the code from BB into BLB… Ugh.

 

Or follow the CSLA model of keeping them separate:

 

Root contains

  LineItemContainer contains

    LineItem contains

      LineItemContainer …

 

Rocky

ajj3085 replied on Thursday, January 17, 2008

Ya, I thought I may have to create such a class.  Not something I'd really want to do.

I moved to the composite pattern because I was running into issues with having a BLB as a parent.. it was basically in the way, so to speak. 

I'm still working on this though, so maybe I'll end up at that... maybe the collection classes could act as a bridge so to speak to between parent and child objects. 

Copyright (c) Marimer LLC