Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Binding POCO Properties

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

like image 615
Kent Boogaart Avatar asked Mar 02 '09 10:03

Kent Boogaart


3 Answers

I wrote Truss to fill the void.

like image 180
Kent Boogaart Avatar answered Nov 14 '22 06:11

Kent Boogaart


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);
like image 8
Bevan Avatar answered Nov 14 '22 07:11

Bevan


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

like image 1
Thomas Eyde Avatar answered Nov 14 '22 05:11

Thomas Eyde