Cascading Lists - CSLA

Cascading Lists - CSLA

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


DivisibleByZero posted on Thursday, February 26, 2009

I have a question concerning CSLA and cascading lists, i.e. lists that are dependent on each other.

Example:
Car Make and Model.

CarMakes : ReadOnlyListBase<CarMakes, CarMake>
  public static CarMakes GetList()

CarModels : ReadOnlyListBase<CarModels, CarModel>
  public static CarModels GetList(CarMake make)


Lets say we also have a business object that will store this information:

CarOrder
   public CarMake Make { get; set }  //CSLA GetProperty/SetProperty
   public CarModel Model { get; set }  //CSLA GetProperty/SetProperty

The Make property is bound to the ComboBox cboMakes SelectedItem
The Model property is bound to the ComboBox cboModels SelectedItem
On my Windows Form we can load up the Makes right away because it is not dependent on anything:

Form_Load:
     _order = CarOrder.NewOrder();
     makesBindingSource.DataSource = CarMakes.GetList();
    orderBindingSource.DataSource = _order;

My question is how should I go about loading the second list? How do I know when the Make has changed?  Do I subscribe to the makesBindingSource.CurrentChanged event and load the BindingSource at that point?

Is there anything else I have to worry about when I bind this second list? Do I need to unbind the orderBindingSource first and then bind the models, then rebind the orderBindingSource?

Any help would be appreciated.

ajj3085 replied on Friday, February 27, 2009

I'd have your CarOrder have a read-only CarMake property.  It's set when you set the model.. which contains information about it's Make. 

Alternately, if your use case really does allow selecting make & model separately, whenever Make is set Model should be blanked.  I'd go with strings personally... I've found this much easier to deal with.  Your validation rule for Make will check the list of possible makes and set a broken rule if the make doesn't exist, and  your model will check the model's list, filtered by the current make. 

This should make your UI much easier too... I went down your path originally.. and it got to be a real pain. 

HTH

DivisibleByZero replied on Friday, February 27, 2009

The car example isn't completely analagous, but I can't just store a string. Besides, that's not what this question was about. The question is how do  I change the lists for the combobox based on the first item changing. I was asking specifically if you'd handle an event such as BindingSource.CurrentChanged or ComboBox.SelectedItem changed. I realize that is putting a lot of logic in the UI layer to refilter the results, but I'm not sure how else to do it.  Possibly adding an event in the CarOrder object called MakeChanged?  Another part of the question was wondering if there is anything I need to worry about when changing the binding list of the second dropdown...such as if I need to unbind the carorder before doing that, etc.?


ajj3085 replied on Friday, February 27, 2009

Well... the best way to get an answer to your question is to post your actual use case.

Anyway, to answer your question, I usually end up watching the ComboBox.SelectionChanged event, and the changing the filter on the "Model"'s combobox's DataSource.  That's not as easy as you would think though, because the event fires when you wouldn't expect it to.. for example when the screen is first displayed.  This can end up resetting your Model's combobox items unexpectedly, which then causes the Model combobox to shove an empty value into the Model property of your BO... and instantely your BO is in a dirty state that doesn't reflect the initally loaded state.

I've handled this by going to Infragistics UltraComboEditor and using flags to indicate that the BO is being bound to the UI and the events that normally filter the Model combobox will not do anything in this case.  This is in WinForms, BTW.  I'm not sure if you're run into the same situation with Wpf though, because the databinding there makes more sense... but I haven't actually done this yet in wpf either.

HTH

DivisibleByZero replied on Friday, February 27, 2009

I don't think the actual use case really matters. Lets say that CarMake has a property called CountryOfOrigin. Maybe there is a business rule in CarOrder that says if the country is USA then you can't choose a certain color. Storing Make as a String "Ford" isn't going to tell me that.


Anybody else have any ideas on this?

Copyright (c) Marimer LLC