ComboBox and Custom Objects

ComboBox and Custom Objects

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


ksirmons posted on Tuesday, June 12, 2007

Howdy,

I have a class, TestRequest (BusinessBase), that contains a custom object _requestor as UserInfo.    UserInfo is a ReadOnlyBase object that holds the requesting doctors first name, last name, and ID.

UserInfo’s behavior is its ability to represent the doctors name in almost any combination, first last or last, first.

I have created a UserInfoList (Readonly List Base) that can get a list of users  -- GetDoctors.

I can set a datasource  for this ComboBox and display a list of doctors to choose from. 

How can I get the databinding to work so I can select an item out of this list and a UserInfo object be sent to the _requestor setter property?

Can an “Object” be selected from a combo box and bound to a property?  Or do I have to use the UserInfo.ID and make my TestRequest use an Integer type for _requestor in place of UserInfo?

 

I hope I am asking the correct question here.

Thank you for any help you can provide.

Keith

Brian Criswell replied on Tuesday, June 12, 2007

You can bind to the ComboBox SelectedItem property, but I would really recommend holding an id in your object and binding to SelectedValue instead.  Holding a reference to the selected item causes an issue with the n-level undo of CSLA.  The undo serializes the selected item's values onto the undo stack instead of making a note that it is the selected item.  So if you change which item is selected, it does not undo properly.

ksirmons replied on Tuesday, June 12, 2007

Thank you.  I don't think that I mentioned it on the original post, but does it make a difference if I am doing this on the web?

 

Keith

Brian Criswell replied on Tuesday, June 12, 2007

N-level undo usually does not factor in to web development, but I would personally suggest that you expose the id.  Maybe some other people will chime in.

ksirmons replied on Tuesday, June 12, 2007

So you would vote for using an integer in my business object to hold the primary key of the doctors user record in place of a user object? 

It would seem much more powerful to treat a doctor as an object instead of as an ID.

 

Keith

Brian Criswell replied on Tuesday, June 12, 2007

There is nothing to say that you cannot look up that doctor based on the id you hold.  What would you do with this doctor reference within your object?

ksirmons replied on Tuesday, June 12, 2007

That is a good question.  So you would advocate when dealing with “relational” objects to store the foreign key as an ID in place of storing the child object?  Is there ever a case where you would want/need to use a Child Object in place of an ID?

 

I could make up a case for storing the Child Object here, but it would be weak…  The “What If” I need to show the doctors name on the page in more than one place would require multiple database trips to get the Doctors name for each instance it is displayed, or create a temporary var to hold it once and use it over the rest of the page.

 Keith

Brian Criswell replied on Tuesday, June 12, 2007

Yes, I store the id for lookups.  In the cases where I have needed to display another piece of the data, I either have the UI hold a reference to the lookup list to use in a combobox or cached within the object.  When using it within the object, I lazy load the list and expose just the parts I want as properties on my object.  So for a name

public string DoctorName
{
    get
    {
       CanReadProperty("DoctorId", true);
       if (_doctorId > 0)
       {
          Doctor doctor = this.Doctors.GetDoctor(_doctorId);    // Lazy-loaded doctors property with my custom GetDoctor by id method
          if (doctor != null)
          {
             return doctor.Name;
          }
       }

       return string.Empty;
    }
}

So store an id in your business object and use the looked up object where you need it.

ksirmons replied on Wednesday, June 13, 2007

Thank you.  I will transition my objects toward this frame of thought. 

One of the reasons I wanted to use a custom object was so I could use a DataFormatString on the UI side to format the name in several standard formats.  (First Last or Last, First or FL)

Would you know how to use the DataFormatString in this situation?  As far as I know, the DataFormatString uses the objects ToString method that is overloaded. 

Here is the other post: http://forums.lhotka.net/forums/thread/15307.aspx that you helped me on as well.

Keith

Brian Criswell replied on Wednesday, June 13, 2007

I think you just have your object implement IFormattable.  The ToString(string) method that you implement handles what format strings are acceptable and what they will mean.

Copyright (c) Marimer LLC