Home > View Post

Updated data-binding friendly script for Data Services

A couple of months ago I posted a script Making webdatagen data-binding friendly that took a .cs file generated by ADO.NET Data Services using webdatagen.exe/datasvcutil.exe and made it data-binding friendly.

David Browne who works for Microsoft in Dallas got in touch having used the script and made a few improvements. Here's the updated script:

$file = $args[0]

$content = [System.IO.File]::ReadAllText($file);

$hash = $content.GetHashCode();

$re = [regex] "\.Collection<"
$content = $re.replace($content, ".ObservableCollection<")

$re = [regex] "this.On(.*?)Changed\(\);"
$content = $re.replace($content, "this.On`$1Changed()/**/;`r`n`t`t`t`tthis.OnPropertyChanged(`"`$1`");")

$re = [regex] "this._(.*?) = value;(\s*?)}"
$content = $re.Replace($content, "this._`$1 = value/**/;`r`n`t`t`t`tthis.OnPropertyChanged(`"`$1`");`$2}")

$re = [regex] "(?m)public partial class (\w*?)`r`n(\s*?){"
$content = $re.Replace($content, "public partial class `$1 /**/: System.ComponentModel.INotifyPropertyChanged`r`n`t{
`t`tpublic event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;


`t`tprotected virtual void OnPropertyChanged(string propertyName)
`t`t{
`t`t`tif (PropertyChanged != null)
`t`t`t{
`t`t`t`tPropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
`t`t`t}
`t`t}



"
)

if ($hash -ne $content.GetHashCode())
{    
    [System.IO.File]::WriteAllText($file, $content);
    Out-Host -InputObject ($file + " updated.");
}

Perhaps the best thing about this new version is it is now idempotent and so it can be run as part of a pre-build script. Nice. David also spotted a little bug where I'd forgotten to escape a '.' so instead the Regex would have treated it as a wildcard.

Thanks David!

Tags: ASP.NET

 
Josh Post By Josh Twist
1:42 AM
31 Jul 2008

» Next Post: Why does Visual Studio keep checking some project files out?
« Previous Post: Yet more on to var or not to var

Comments are closed for this post.

© 2005 - 2017 Josh Twist - All Rights Reserved.