Spring.NET bi-directional Databinding

I’ve decided Spring.NET has a ton of features, that I would consider it over Windsor now.
One thing that is very cool is that they have a web framework that supports the bi-directional binding.
If you download it, it comes with a full set of examples to see how it works.

ie. here is a sample page:

public partial class DataBinding_RobustEmployeeInfo_Default : Spring.Web.UI.Page
{
    private EmployeeInfo employee = new EmployeeInfo();

    public EmployeeInfo Employee
    {
        get { return employee; }
    }

    /// <summary>
    /// In order to declare data bindings, all you need to do is
    /// override InitializeDataBindings method and add all necessary
    /// data bindings to the BindingManager.
    /// </summary>
    protected override void InitializeDataBindings()
    {
        BindingManager.AddBinding("txtId.Text", "Employee.Id")
            .SetErrorMessage("ID has to be an integer", "id.errors");
        BindingManager.AddBinding("txtFirstName.Text", "Employee.FirstName");
        BindingManager.AddBinding("txtLastName.Text", "Employee.LastName");
        BindingManager.AddBinding("txtDOB.Text", "Employee.DateOfBirth")
            .SetErrorMessage("Invalid date value", "dob.errors");
        BindingManager.AddBinding("txtSalary.Text", "Employee.Salary", new CurrencyFormatter())
            .SetErrorMessage("Salary must be a valid currency value.", "salary.errors");
        BindingManager.AddBinding("rbgGender.Value", "Employee.Gender");
        BindingManager.AddBinding("ddlAddressType.SelectedValue", "Employee.MailingAddress.AddressType");
        BindingManager.AddBinding("txtStreet1.Text", "Employee.MailingAddress.Street1");
        BindingManager.AddBinding("txtStreet2.Text", "Employee.MailingAddress.Street2");
        BindingManager.AddBinding("txtCity.Text", "Employee.MailingAddress.City");
        BindingManager.AddBinding("txtState.Text", "Employee.MailingAddress.State");
        BindingManager.AddBinding("txtPostalCode.Text", "Employee.MailingAddress.PostalCode");
        BindingManager.AddBinding("txtCountry.Text", "Employee.MailingAddress.Country");
    }

    protected void Page_Load(object sender, EventArgs e)
    {}

    protected void btnSave_Click(object sender, EventArgs e)
    {
        // do something with employee such as:
        // employeeDao.Save(Employee);

        Debug.Write(Employee);
    }
}


Then if someone clicks a button it binds those fields to the employee. This is very clean and as you can see it also lets you setup some validation with error messages.

Spring.NET is an entire Application framework, including DI/IoC, AOP, NHibernate support, ADO.NET support, Transactional support, as well as a web framework.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s