views:

446

answers:

2

I've got a person object with a name and age property that implements INotifyPropertyChanged. I want to hook this object up to an ASP.NET form so that the 'name' and 'age' properties bind to textboxes in a way that, when changes happen in either place (in the control or in the object) the other will get updated.

Do I create an intermediary class that listens to each textbox change events and the objects change events and handle the updates between them? What's the best way to do this?

I'm unclear on how to get business objects and the UI talking to each other.

+1  A: 

I've stressed over this exact problem a lot.

The short answer is, yes, an intermediate item.

The trick is to NOT write ANY code per control. You should be able to place a GUI control on the screen (That may or may not take code), and then bind your business logic to it through a generic binding mechanism.

I have defined the bindings through XML, through properties files, and through constant arrays--there are a million ways...

You probably have to write code per TYPE of object bound (a listbox binds differently than a text control) and you may have to write validators (but specifying the parameters to the validators and which control the validators bind to should also be done in data)

Now all that said, I'd be really surprised if some data-driven auto-binding mechanism didn't already exist, Microsoft has been into that since VB first came out (although their implementations used to be pretty inflexible, I'm sure they do a better job now).

I'm very insistent about the 0 lines of code per control because my job has typically involved configuring complex devices with dozens of pages of controls. A typical client/server system will have 7(!) lines of code PER CONTROL just to transport data from the DB, to the server, to the client, to the screen and back (this is a minimum for plain ole "dumb" code with no smart binding tricks).

0LOC/control may not be a requirement for everyone, but it's a good goal.

Comment response: I've done most of my stuff manually in Java, so I'm not sure I can be too much help with the specifics.

Searching for C# and binding gave me this which looks promising, although it may be binding straight to a database which is too much IMO, it should bind to a business object, but the concepts should be the same.

One way to create the bindings at first is to manually instantiate binding objects... (Please excuse my Java)

TextControl textCtrl1=new TextControl("Name Goes Here");
new TextBinder(textCtrl1, personObject, nameField);

In Java, that second line gets tricky. When you are binding to a particular field, you HAVE to use reflection to find the setter and getter for that field of the personObject. In C# I think it should be easier.

Anyway, the binder should add itself as a listener to the control and the object, then forward changes back and forth.

Does that help any?

Edit2: As you noticed, the hard part is noticing when your property is updated. Luckily, that is optional. More often than not, you don't need to update the component once the object is set (I had to deal with this a few times when I had distributed UIs that could update each other).

So, if you assume your object won't change, the "Binding" has to do the following:

  • get the value from the property and set it in the component.
  • add itself as a listener to the component.
  • store the property/object (if you can manipulate properties, you're set here. If not, you need to store the object and property name, and use reflection)
  • bail and wait for an "updated" event from your component.

When you get the update from your component: - store the value in the property. - You may want to set an "Updated" flag or store the original so that if you iterate through all the binding components, you can tell if any updates need to be saved/enable the "ok" button.

Your object should always be pretty much up-to-date now.

As you build a form, you may want to put all your binding controls into a collection so that you can do a few other operations...

  • A "Save" operation could call each binding control and tell it to copy from the control to the property, that way you don't need to use a listener.
  • A "Reset" operation can reset all the controls to their original value.
  • A "Test" operation can ask each control if it's been updated. . etc

The neat thing about doing it this way is that every "Operation" you wish to add is pretty trivial to add, but automatically affects the entire UI.

You probably also want a little object hierarchy of controls with an abstract base "bind" class, then a specific binder for each type of control (text field, number field, date, spinner, table, pulldown)--I think that's about it.

This can be very simple, but gains complexity rapidly. Try it with a text field and see what you can do. A simple text binding object should just be like 5 lines of code if you can pass "properties" around in C#...

Bill K
Thanks, Bill! Can you point me to someplace I can see some examples?
Gern Blandston
So the TextControl represents a textbox and TextBinder represents an object that holds the single relationship between control and business object? I think I need to do some reading on the 'add itself as a listener to the control and object' bit.
Gern Blandston
Correct. It's easy to add itself to the control as a listener. Fairly easy to write changes from the control to the property. Hard to listen to the property for updates unless C# has built-in support. I had to be tricky with this. Hmm, may need another edit to explain.
Bill K
I think I found a reference. This: http://msdn.microsoft.com/en-us/library/z919e8tw.aspx should allow be in your binding object's event code--it should be used to update your business logic.
Bill K
Is the 'add itself to the control as a listener' bits a Java thing? I guess this is the crux of my question; how do I get the object to listen to the control for event changes? thanks for you help!
Gern Blandston
http://msdn.microsoft.com/en-us/library/aa645739(VS.71).aspx
Bill K
Thanks again, Bill! I appreciate your help!
Gern Blandston
A: 

Okay, totally separate answer. As I told you, I'm not very up-to-date with C# technologies, but from what I've heard, LINQ may do this entire job for you.

In fact, LINQ may be made to do exactly what you are trying to do. It doesn't exist in Java, so that's why I gave you the "Manual" version in the other answer.

The comment at the bottom of this page: http://msdn.microsoft.com/en-us/library/z919e8tw.aspx alludes to a better way.

Bill K