---
title: "ANN: Optimized automatic control binder is out - and its fast!"
url: "https://osherove.com/blog/2004/9/22/ann-optimized-automatic-control-binder-is-out-and-its-fast.md"
canonical: "https://osherove.com/blog/2004/9/22/ann-optimized-automatic-control-binder-is-out-and-its-fast.html"
published: "2004-09-22T14:22:00"
updated: "2004-09-22T14:22:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "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…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

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

By Roy Osherove September 22, 2004 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published September 22, 2004; technical details may be outdated.

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](http://weblogs.asp.net/rosherove/archive/2004/08/31/222948.aspx). 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!

 features: ([see code examples on how to use it](http://weblogs.asp.net/rosherove/archive/2004/08/31/222948.aspx))

- 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.

[Download it here](http://files.osherove.com/downloads/automaticbinding.zip)

the download contains a single class file and also a simple binding demo with Northwind database. Very easy and simple. Have fun!

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- Oct 3, 2004

    ### [Upcoming talks and webcasts I'm involved with](https://osherove.com/blog/2004/10/3/upcoming-talks-and-webcasts-im-involved-with.html)

- Oct 3, 2004

    ### [DataSet hell - "Failed to enable constraints. One or more rows contain values...."](https://osherove.com/blog/2004/10/3/dataset-hell-failed-to-enable-constraints-one-or-more-rows-c.html)

- Sep 21, 2004

    ### [One treeview I'm not going to download](https://osherove.com/blog/2004/9/21/one-treeview-im-not-going-to-download.html)

- Sep 15, 2004

    ### [Finding a Palindrome](https://osherove.com/blog/2004/9/15/finding-a-palindrome.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
