Click to See Complete Forum and Search --> : Collection Classes, best way to populate?


Dmorley
July 18th, 2005, 05:32 AM
Hi,

This is an object design question - where do you think the best place is for the following functionality to be implemented.

Class 'Buyer'. Represents a product buyer.

Class 'Buyers'. Is a type-safe collection class of Buyer, inheriting the ReadOnlyCollectionBase class.

Both of these classes exist in my middle-tier business logic, which has access to the 'data access layer' that queries SQL Server. In my client GUI, i'm using the following code to load a combo box.

As you can see in the code, i've created a method that is part of the Buyers collection class that populates the collection.

oBuyers.Populate();


private void LoadBuyers(int BuyingDepartmentID)
{
Buyers oBuyers = new Buying.Buyers();
Utilities.ComboItem oCombo;

try
{
this.cboBuyers.Items.Clear();

// Create a blank item at the top of the list
oCombo = new Utilities.ComboItem("", -1);
this.cboBuyers.Items.Add(oCombo);

oBuyers.Populate(BuyingDepartmentID);

foreach (Buyer oBuyer in oBuyers)
{
oCombo = new Utilities.ComboItem(oBuyer.FullName, oBuyer.BuyerID);
this.cboBuyers.Items.Add(oCombo);
}

}


Should this Populate method be part of the Buyers class? Or should I implement this in another class that returns type 'Buyers'?

Any advice appreciated!

Thanks

torrud
July 18th, 2005, 05:40 AM
You could implementing the factory pattern, what means you have classes which instantiate and initialize your objects. So you do not need to call a new constructor or something like that in your presentation layer. It helps you to keep your gui code clean from business instructions.
In the factory class you would instantiate and load your collection.

Dmorley
July 18th, 2005, 05:46 AM
Ah yeah - factory pattern, that's what I was after....I knew there was a correct terminoligy for this!

That's what I was thinking - didn't want to be calling the Populate method each time & would prefer to keep my collection classes free from this sort of code.

Thanks!

torrud
July 18th, 2005, 07:26 AM
I am looked into my link list and found an interesting article about factories and .NET. Maybe you are interested in.Then you should have a look here (http://msdn.microsoft.com/msdnmag/issues/03/03/DesignPatterns/default.aspx).