Skip Navigation LinksHome > View Post
Making webdatagen data-binding friendly

If you haven't looked at ADO.NET Data Services (formerly project astoria) yet then you should. It's awesome. Start here: astoria.mslivelabs.com.

If you have looked at this stuff and you're a big fan of data-binding then, like me, you were probably a little dissapointed to see that the webdatagen.exe util (now called datasvcutil.exe in .NET 3.5 SP1 beta 1) doesn't generate data-binding friendly classes for you.

Not to worry - there isn't much that PowerShell and regex can't fix. Just point this script at your generated (c#) file and it will implement INotifyPropertyChanged and use ObservableCollections where possible for you:

$content = [System.IO.File]::ReadAllText("PATH_TO_YOUR_GENERATED_FILE_HERE.cs");

$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}



"
)

[System.IO.File]::WriteAllText("PATH_TO_YOUR_OUTPUT_FILE_HERE.cs", $content);


I hope that by RTM the generated classes will have some data-binding friendly pieces but this should see you through until we get there.

Josh Post By Josh Twist
10:08 AM
13 Jun 2008

Comments:

Posted by zac morris @ 01 Jul 2008 6:26 AM
Hi Josh,

Thanks for posting this script. It fills a big whole in the DataSvcUtil tool.

I ran in to this problem, maybe you've seen it too?
(Maybe this is why MS hasn't implemented it yet?)

NullReferenceException thrown with this stack trace:
System.Windows.dll!System.Windows.Data.BindingExpression.SourcePropertyChanged(object sender = {WattLink1Model.Site}, System.ComponentModel.PropertyChangedEventArgs args = {System.ComponentModel.PropertyChangedEventArgs}) + 0x6e bytes
System.Windows.dll!System.Windows.Data.WeakPropertyChangedListener.PropertyChangedCallback(object sender = {MySvcUtilGeneratedClass}, System.ComponentModel.PropertyChangedEventArgs args = {System.ComponentModel.PropertyChangedEventArgs}) + 0x59 bytes
> MyProject!MySvcUtilGeneratedClass.OnPropertyChanged(string propertyName = "Name") Line 1231 + 0x1a bytes C#



Posted by Josh @ 01 Jul 2008 12:13 PM
Hi Zac,

No I haven't seen that issue. Admittedly I've not put the script through the most rigorous of testing but it's worked every time for me.

Can you share the original .cs file that's causing the problem? My e-mail is on the contact us page.

Post a comment:

Name  

E-mail (never shared)

URL

Comments  

Captcha ImageRefresh Image
What's this?
Enter code above