Recommended pattern?

Recommended pattern?

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


ajj3085 posted on Thursday, September 28, 2006

OK,

Now I'm implementing my LineItems and various LineItem classes.  The basic design is that you can't create any LineItem directly, you do so by calling various methods on the LineItems class, like so:

public sealed class LineItems : BusinessListBase<LineItems, ILineItem> {
        public ILineItem AppendItem( ProductPriceInfo product );
        public ILineItem InsertItem( ProductPriceInfo product, int position ) ;

        public ILineItem AppendItem( string noteText ) ;
        public ILineItem InsertItem( string noteText, int position );

        public ILineItem InsertSubTotal( int position );
        public ILineItem AppendSubTotal();

        public ILineItem GroupItems( List<ILineItem> items );
}

Behind the scenes, I have a LineItemFactory to which the above calls are delegating.  First, I've hit a problem with the factory; I don't have anything to pass it so that it can 'figure out' that a SubTotal line item is what is required.  That's fine, but I have a small concern that there may be a type of line item in the future that also doesn't need any info. 

So first, is that something I shouldn't worry about yet anyway?  Second, perhaps I don't need  a factory here anyway?  Finally, should the list be handling this, or perhaps I should make the factory public and let the UI handle inserting?  

Thanks
Andy

RockfordLhotka replied on Thursday, September 28, 2006

I wonder if you shouldn't expose a set of overloaded factory methods directly to the UI, allowing the UI to create the ILineItem directly. I assume you can detect the right type of object based on whether you get a product, string or no parameter?

ajj3085 replied on Monday, October 02, 2006

Thanks for the advice, this is what I have done.

My only question though is what happens if there's ever a line item type which can't be uniquely identified based off of the parameters?

Also, the UI will now be calling CreateLineItem() without parameters when it wants a subtotal line item.  Still seems a bit odd, but is that the recommended way?

Thanks
Andy

malloc1024 replied on Monday, October 02, 2006

You will need to send a parameter to your factory.  The factory could return a null object of a line item if no parameter is given; however, for all other cases, you will need to send a parameter for the reasons you mentioned previously.  If you can’t think of a parameter that distinguishes sub total from other line items I would probably go with a enum.  I am not sure if this is the proper way to go, but this is what I have done in the past.  It works and it is better than passing no parameters to get a sub total object.

malloc1024 replied on Thursday, September 28, 2006

It seems that your LineItems class contains some unnecessary methods.   I would eliminate all AppendItem methods.  The InsertItem method should append by default if no position is provided.  I would have 2 overloaded InsertItem classes.  One that takes an ILineItem object and the other that takes an ILineItem object and a position.   I would use the factory in the UI to create you ILineItem objects.  This will reduce the pollution inside your LineItems class and make it easier to use.

Copyright (c) Marimer LLC