Follow @RoyOsherove on Twitter

ANN: Optimized automatic control binder is out - and its fast!

Last month I posted about a cool way we came up with to automatically bind all the controls on our application forms to various data sources. That solution was cool, but it was slow. Because it used binding. And because I made it in like an hour, and because I didn't optimize the various loops inside it and so on..
 
Well - here's the new - optimized version of the ControlBinder class.
By optimized I mean that with the old class we ran it on a form with about 200 controls and it took about 5 seconds to bind. With the new version it takes 0.6 seconds on the first run. In the next runs (that is - you closed the form and then reopened it and then called bind again) it takes 0.3 seconds. Yeah - its cool and now its even usable!
  • Caching, Caching, Caching: On the first run on the form all attributes, member infos and bindable controls are saved to a cache hidden from the developer. On the next runs the cache is used to bind controls, without the need to use reflection more than once on each form. This way even if you closed the form and reopened it - binding it again will be much faster.
  • Categories: Bind multiple controls on the form to multiple data sources by providing a "category" attribute value and calling "bind" specifying a category and a data source.
  • ControlCoordinate class: this one was a bitch. There's no easy way to find a control on a form in win forms 1.1. That is - each control's Index on the containing control can be retrieved, but you can't use that index later to find the control on the form instance. This is because control collections are hierarchical - so if you want to find a control which resides in a control panel on your form you'd have to do something like form.Controls[0].Controls[0] to get that control. Easy huh? Well - I made a ControlCoordinate class which can take any control and save a "marker" to it, which can later be retrieved easily by calling Coordinate.FindControl(form). Very simple and easy. Its all in there. Coordinates are saved in an inner cache to so that after we know what controls actually have various attributes we can ignore all the other controls and zoom in directly on each control using its coordinate. Phew. That took a while to find out how to do (at first I did not believe the solution has to be this in-elegant)
  • BindToValueList: You can bind combos as well with this special attribute
  • Extensibility: Its easy to create your own attributes and control types with special bindings. The object model contains an extensible abstract factory pattern of specific control binder implementations per attribute type or control type.
the download contains a single class file and also a simple binding demo with Northwind database. Very easy and simple. Have fun!

Out of Country - Croatia here we come!

MbUnit 2.21.1.0 RC1 released (and some nifty nifty features!)