Home > View Post

Making webdatagen data-binding friendly

UPDATE - update to this script available here: Updated data binding friendly script for Data Services.aspx.

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`tif (PropertyChanged != null)
`t`t`t`tPropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));


[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

» Next Post: Thanks to NxtGenUG - WPF and Databinding bits and pieces
« Previous Post: Speaking at NxtGenUG's FEST '08

Comments are closed for this post.

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.

Posted by Peter @ 06 Feb 2009 2:46 PM
Hi Josh,
Nice, clean simplicity.

It's been 8 months and a couple updates from Microsoft now. Is this still a valid approach? Is it still how you recommend extending the classes?


Posted by Peter @ 11 Feb 2009 1:48 PM
Hi Josh, this is pretty slick.

INotifyPropertyChanged passes through correctly, but I'd also like to use IDataErrorInfo to handle validation on order to implement an MVVM type pattern. Did you implement IDataErrorInfo?

If so, how did you pass through the fields to the data service without passing
public string Error
which you need to declare for IDataErrorInfo?


Posted by 92 @ 19 Mar 2009 5:39 PM

Posted by Roger @ 28 May 2009 10:03 AM
Can I 'second' on Josh's question? Any changes, is this still the best approach?


Posted by josh @ 28 May 2009 12:04 PM
Hi Roger,

Check out the udpate at the top - added following your comment.


© 2005 - 2022 Josh Twist - All Rights Reserved.