BusinessBase and events

BusinessBase and events

Old forum URL:

DivisibleByZero posted on Thursday, March 26, 2009

I have a BusinessBase class which has a few properties:

Root : BusinessBase<Root>
   int Foo
   int Bar

In my UI, I need to populate a dropdown with all the possible values for Foo

cboFoo.DataSource = FooList.GetList();

I also need to populate a list of Bar's, but Bar is dependent on Foo:

cboBar.DataSource = BarList.GetList(fooId);

Should I add an event to my root class called FooChanged and subscribe to that in order to know when I need to repopulate the Bar dropdown? I don't want to write the code in the combo box selected item changed handler.

RockfordLhotka replied on Thursday, March 26, 2009

This is a common question and there are several threads on the forum about populating combobox controls and how to manage NameValueListBase objects.

My basic answer is this.

If your NVL is global (doesn't change based on other fields) then you should reference it globally:

var list = MyListOfItems.GetList();

If your NVL is different depending on the scenario, then it belongs to that scenario. In your example, FooList is global, but BarList is scenario dependent. So I'd expose a BarList property off Root, so the UI would get the appropriately filtered list from Root.

This is also useful, because Root may want to validate the value of Bar, which means verifying that it is in the appropriately filtered list. By maintaining its own appropriately filtered list, the Root object can easily implement its validation rule.

Then all you need to do in Root is make sure to raise PropertyChanged for BarList any time Foo changes. Though in Windows Forms that is probably not necessary since any PropertyChanged event refreshes all bound fields - but you'll have to test to see what works.

Copyright (c) Marimer LLC