How many lines of code have we all written to set properties from a web form to a business object. 1000's of lines of code and countless wasted hours developing and debugging typos in field names. After years it gets old.

I found a article (http://msdn.microsoft.com/en-us/library/aa478957.aspx) on Microsoft's site about doing just what my goal was. My goal was to turn the hundreds of lines of code in application setting UI properties into business object properties.

For example;

Normal code;

// create entity
Person p = new Person(1, "John", "Doe");

p.FirstName = FirstName.Text;
p.LastName = LastName.Text;

// more code...
Goal code;

// create entity
Person p = new Person(1, "John", "Doe");

// bind
Binder.BindToControl(p, Page);

Enter the Binder class that I have so far after about 2 hours of R&D and playing around with different setups.

using System;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Pixelect.Example.Web
{
    public static class Binder
    {
        public static void BindToControl(object entity, Control control)
        {
            // ensure objects
            if (entity == null || control == null) return;

            // get properties
            PropertyInfo[] infos = entity.GetType().GetProperties();

            // loop through each property info object
            foreach (PropertyInfo info in infos)
            {
                // if can read property
                if (info.CanRead)
                {
                    // find matching control
                    Control matchingControl = control.FindControl(info.Name);

                    // check for match
                    if (matchingControl == null) continue;

                    // process type
                    if (matchingControl is TextBox)
                        ((TextBox) matchingControl).Text = Convert.ChangeType(info.GetValue(entity, null), typeof(string)).ToString();

                    if (matchingControl is Literal)
                        ((Literal)matchingControl).Text = Convert.ChangeType(info.GetValue(entity, null), typeof(string)).ToString();

                    if (matchingControl is Label)
                        ((Label)matchingControl).Text = Convert.ChangeType(info.GetValue(entity, null), typeof(string)).ToString();

                    if (matchingControl is ListControl)
                    {
                        ListItem li = ((ListControl)matchingControl).Items.FindByValue(info.GetValue(entity, null).ToString());
                        if (li != null)
                        {
                            foreach (ListItem item in ((ListControl)matchingControl).Items)
                                item.Selected = false;

                            li.Selected = true;
                        }
                    }
                }
            }
        }
        public static void BindToObject(Control control, object entity)
        {
            // ensure objects
            if (entity == null || control == null) return;

            // get properties
            PropertyInfo[] infos = entity.GetType().GetProperties();

            // loop through each property info object
            foreach (PropertyInfo info in infos)
            {
                // if can read property
                if (info.CanWrite)
                {
                    // find matching control
                    Control matchingControl = control.FindControl(info.Name);

                    // check for match
                    if (matchingControl == null) continue;

                    // process type
                    if (matchingControl is TextBox)
                        info.SetValue(entity, Convert.ChangeType(((TextBox)matchingControl).Text, info.PropertyType), null);

                    if (matchingControl is ListControl)
                    {
                        if (((ListControl)matchingControl).SelectedItem != null)
                            info.SetValue(entity, Convert.ChangeType(((ListControl)matchingControl).SelectedItem.Value,  info.PropertyType), null);
                    }
                }
            }
        }
    }
}

This class allows easy binding between web form UI elements and entity objects where the UI control ID is equal to the entity property name. This is just a first draft of the class and I will be updating the class to support the bulk of the toolbox items in visual studio. I will also integrate the code from the Microsoft article to support known properties on unknown UI object types.

Then hopefully.... we can cut down on all that error prone and boring code that we all must type to bind UI elements to back end objects.

Enjoy