views:

870

answers:

5

Are there any data binding frameworks (BCL or otherwise) that allow binding between any two CLR properties that implement INotifyPropertyChanged and INotifyCollectionChanged? It seems to be it should be possible to do something like this:

var binding = new Binding();
binding.Source = someSourceObject;
binding.SourcePath = "Customer.Name";
binding.Target = someTargetObject;
binding.TargetPath = "Client.Name";
BindingManager.Bind(binding);

Where someSourceObject and someTargetObject are just POCOs that implement INotifyPropertyChanged. However, I am unaware of any BCL support for this, and am not sure if there are existing frameworks that permit this.

UPDATE: Given that there is no existing library available, I have taken it upon myself to write my own. It is available here.

Thanks

A: 

AutoMapper can copy values between two instances, but you have to write your own code to make this happen automatically.

Thomas Eyde
+1  A: 

Maybe Bindable LINQ or continuous linq can help here. If you're trying to add model properties that are actually "derived properties" of your actual, updating data, to make it easier for you UI to bind to, these two frameworks should help.

Kurt Schelfthout
+5  A: 

I'm not aware of any library that does this - but you could write your own fairly easily.

Here's a basis I knocked up in a few minutes that establishes two way data binding between two simple properties:

public static class Binder
{

    public static void Bind(
        INotifyPropertyChanged source,
        string sourcePropertyName,
        INotifyPropertyChanged target,
        string targetPropertyName)
    {
        var sourceProperty
            = source.GetType().GetProperty(sourcePropertyName);
        var targetProperty
            = target.GetType().GetProperty(targetPropertyName);

        source.PropertyChanged +=
            (s, a) =>
            {
                var sourceValue = sourceProperty.GetValue(source, null);
                var targetValue = targetProperty.GetValue(target, null);
                if (!Object.Equals(sourceValue, targetValue))
                {
                    targetProperty.SetValue(target, sourceValue, null);
                }
            };

        target.PropertyChanged +=
            (s, a) =>
            {
                var sourceValue = sourceProperty.GetValue(source, null);
                var targetValue = targetProperty.GetValue(target, null);
                if (!Object.Equals(sourceValue, targetValue))
                {
                    sourceProperty.SetValue(source, targetValue, null);
                }
            };
    }
}

Of course, this code lacks a few niceties. Things to add include

  • Checking that source and target are assigned
  • Checking that the properties identified by sourcePropertyName and targetPropertyName exist
  • Checking for type compatibility between the two properties

Also, Reflection is relatively slow (though benchmark it before discarding it, it's not that slow), so you might want to use compiled expressions instead.

Lastly, given that specifying properties by string is error prone, you could use Linq expressions and extension methods instead. Then instead of writing

Binder.Bind( source, "Name", target, "Name")

you could write

source.Bind( Name => target.Name);
Bevan
I was actually asking because I was considering writing my own. Didn't want to reinvent the wheel and all...thanks.
Kent Boogaart
Update: I have linked to my library in the question.
Kent Boogaart
A: 

If you defined your properties as DependencyProperty's you could do it. Both WF and WPF have an implementation of it (first link is for WPF. For WF it is this one) so you need to decide which to use - but both should suffice for your needs.

Robert MacLean
DependencyProperty implies inheriting from DependencyObject, which is not a POCO.
Kent Boogaart
+1. In other words, "You can't with poco's, only DPs."
Will
Eh? Not sure what your logic is for voting this up Will. Neither suggestion will suffice for my needs as neither suggestion involves POCOs. Another post has already shown you can do it with POCOs - I'm just asking for a framework that does the hard lifting.
Kent Boogaart
+5  A: 

I wrote Truss to fill the void.

HTH, Kent

Kent Boogaart