Strongly Typed Databindings in SlxWeb
Posted By: nicocrm on June 13th, 2008 in Uncategorized
No Gravatar

I wanted to share a useful little trick to avoid typos when doing the bindings on a custom smart part.

The default (in the code generated by the AA) looks like this:

Sage.Platform.WebPortal.Binding.WebEntityBinding nmeContactNameNamePrefixBinding = new   Sage.Platform.WebPortal.Binding.WebEntityBinding("Prefix", nmeContactName, "NamePrefix");
BindingSource.Bindings.Add(nmeContactNameNamePrefixBinding);

You can also write it a bit more succinctly as this, but it still requires you to remember how to spell the entity property as well as the control property:

BindingSource.Bindings.Add(new WebEntityBinding("Prefix", nmeContactName, "NamePrefix"));

I like it like this, where the Intellisense can do the autocomplete for me:

this.BindingSource.Bindings.Add(t.CreateBinding(s => s.Cust

To this end I have this tiny helper class:

public class TypedWebEntityBindingGenerator<TEntity>
{
    public WebEntityBinding CreateBinding<TComponent, TProperty, TProperty2>(Expression<Func<TEntity, TProperty>> entityProperty, 
        TComponent component,
        Expression<Func<TComponent, TProperty2>> componentProperty)
    {
        var propFrom = BuildPropertyAccessString((MemberExpression)entityProperty.Body);
        var propTo = (PropertyInfo)((MemberExpression)componentProperty.Body).Member;
        return new WebEntityBinding(propFrom, component, propTo.Name);
    }

    private String BuildPropertyAccessString(MemberExpression memberExpression)
    {
        String b = "";
        
        if (memberExpression.Expression is MemberExpression)
            b = BuildPropertyAccessString((MemberExpression)memberExpression.Expression) + ".";
        return b + ((PropertyInfo)memberExpression.Member).Name;
    }
}

You will have to set up your project to target the .NET 3.5 framework.

This *may* also requires you to enable .NET 3.5 in the web.config.  YMMV.  To enable .NET 3.5 you basically replace all occurrences of 1.0.61025.0 with 3.5.0.0 and add the following snippet under the assemblyBinding element in web.config:

<dependentAssembly>
    <assemblyIdentity name="System.Web.Extensions" culture="neutral" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.61025.0"
                     newVersion="3.5.0.0"/>
</dependentAssembly>

2 Responses to “Strongly Typed Databindings in SlxWeb”

  1. Rich EatonNo Gravatar says:

    Mark,

    Once again as I follow your blog your providing great tips for working with the new web client..

    Thanks

  2. Nicolas GallerNo Gravatar says:

    Does not work anymore on 7.5.1, unless you do the control as a web application (i.e. pre-compiled), which is a lot more painful to develop as you have to restart the web site every time you want to test a change.

Leave a Reply