Programming, technology, and CRM – from a Belgian programmer exiled to Missouri
  • rss
  • Home
  • Soft Gallery
    • autosvnbackup.sh
    • VBScript Snippets
  • Contact Me
  • Welcome

Strongly Typed Databindings in SlxWeb

Nicolas Galler | June 13, 2008

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>
Categories
Uncategorized
Comments rss
Comments rss
Trackback
Trackback

« Creating and Displaying a Group Programmatically Random Pitfalls of Saleslogix Web Client »

2 Responses to “Strongly Typed Databindings in SlxWeb”

  1. Rich Eaton says:
    June 18, 2008 at 4:25 am

    Mark,

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

    Thanks

  2. Nicolas Galler says:
    May 14, 2009 at 11:19 am

    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

Click here to cancel reply.

Categories

  • Dojo (1)
  • Experiments (4)
  • Force.com (2)
  • Interesting (1)
  • Javascript (3)
  • MSCRM (1)
  • Programming (63)
  • Rant (3)
  • Saleslogix (41)
  • Tricks (8)
  • Uncategorized (32)

Post History

  • 2011
    • January (3)
    • February (2)
    • March (1)
  • 2010
    • January (3)
    • March (3)
    • April (2)
    • August (2)
    • October (4)
    • November (1)
    • December (2)
  • 2009
    • March (2)
    • April (1)
    • May (3)
    • June (3)
    • July (1)
    • September (3)
    • October (2)
    • December (5)
  • 2008
    • January (9)
    • February (4)
    • March (9)
    • April (1)
    • May (5)
    • June (8)
    • July (1)
    • August (2)
    • September (1)
    • November (1)
    • December (3)
  • 2007
    • January (3)
    • February (7)
    • March (1)
    • April (3)
    • May (6)
    • June (2)
    • July (1)
    • August (2)
    • September (5)
    • October (3)
    • November (5)
    • December (4)
  • 2006
    • January (2)
    • September (1)
    • November (3)
    • December (4)
  • 2005
    • April (1)

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox