Tired of the need of looking up property names when you declare data bindings? Sick of mistyping a property name when you bind and not discovering the mistake until you run your application? Have a look at Strongbind.
Strongbind vs traditional data binding
Traditional data binding is typically declared like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Assume we have a binding source with | |
// a description, and a text box | |
// This binds the Text property of the text | |
// box to the Description property of the source | |
textBox.DataBindings.Add("Text", bindingSource, | |
"Description"); |
The same declaration in Strongbind is written as follows:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using(BindingScope scope = new BindingScope()) | |
{ | |
// Create bindables | |
IBusinessObject bindableSource = scope.CreateSource(bindingSource); | |
TextBox bindableTarget = scope.CreateTarget(textBox); | |
// Declare bindings | |
Binder.Bind(bindableSource.Description) | |
.To(bindableTarget.Text); | |
} |
Behind the scenes of Strongbind
To achieve this strongly typed data binding, Strongbind uses a technique known as proxying. Strongbind dynamically generates a proxy for your business object and your control, and uses the proxies to intercept the calls to the property getters during runtime to declare the data binding. Hence, you need to declare a bindable source and bindable target first to create the proxies, and then use these proxies during the binding declaration. You will get a runtime error if you try to use your real objects when declaring data bindings.Limitations of Strongbind
Although Strongbind makes data binding a far more declarative process, the library does have its limitations.Controls containing ActiveX components are not supported. If the control containing an ActiveX component is a custom control created by you, you can get around it by declaring an interface for
the control and specifying that as the type to use when declaring your binding source:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The control interface | |
// (must inherit IBindableComponent) | |
public interface IBindableAXControl | |
: IBindableComponent | |
{ | |
string Text { get; set; } | |
} | |
// Declare the binding target this way within a | |
// binding scope | |
IBindableAXControl source = | |
scope.CreateTarget(control); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The business object interface | |
public interface IBusinessObject | |
{ | |
string Description { get; set; } | |
} | |
// Declare the binding source this way within a | |
// binding scope | |
IBusinessObject bindableSource = | |
scope.CreateSource(source); |
Apart from these two issues, which can be worked around in most cases, Strongbind should work flawlessly. If it does not, please let me know.
Where do I get it?
Strongbind is an open source project hosted at Github. To get the latest version, check out the code from its repository.Strongbind is still in an early development stage, so no releases have been created yet. I still encourage you to check out and start using the library as soon as possible, though. A beta will be released as soon as I feel comfortable doing it.
If you want to contribute to Strongbind, I happily welcome you to do so.
Bind away!