Use T4 code generation to add INotifyPropertyChanged to ADO.Net Data Services (Astoria) Entities

By Rob on Thursday, March 26, 2009

3 Comments

Filed Under: .Net

The current version of ADO.Net Data Services (Astoria) for Silverlight generates some extremely Plain Old CLR Objects. As Shawn Wildermuth notes, you dont even get INotifyPropertyChanged support. You might need this if you want to databind directly to your Astoria entities, but if you’re doing serious Silverlight work, it pays to wrap your entities up in a ViewModel. Even if you’re doing this, having INotifyPropertyChanged support can really help – for example, instead of saving every single entity on your model, you only need to fire the ones that change. For example:

entity.PropertyChanged += (sender, e) => dataContext.UpdateObject(sender);

Pretty soon, this functionality will be built into Astoria. In the meantime, however, it’s possible to add this functionality thanks to the partial methods in the web reference-generated entity. When you add an Astoria web reference to a silverlight project, the two most interesting things that get added (expand the service reference to see these files) are:

Reference.cs

This is the C# file that contains all of the code relating to the service reference. Look inside and you will see your DataContext and all of your Entities. Note that your entities don’t fire property changed events, but they do define partial methods:

        
    public string City
    {
        get
        {
            return this._City;
        }
        set
        {
            this.OnCityChanging(value);
            this._City = value;
            this.OnCityChanged();
        }
    }
    private string _City;
    partial void OnCityChanging(string value);
    partial void OnCityChanged();

service.edmx

This is an XML file that defines, among other things, each of the entities in your service reference:

      <EntityType Name="Address">
        <Key>
          <PropertyRef Name="ID" />
        </Key>
        <Property Name="ID" Type="Edm.Int32" Nullable="false" />
        <Property Name="Line1" Type="Edm.String" Nullable="true" />
        <Property Name="Line2" Type="Edm.String" Nullable="true" />
        <Property Name="City" Type="Edm.String" Nullable="true" />
        <Property Name="Country" Type="Edm.String" Nullable="true" />
      </EntityType>

This XML file serves as the perfect source for our data generation template!

DataServiceINotifyPropertyChanged

If you’re not familiar with T4 code generation and it’s integration into visual studio, now would be a good time to go and read Oleg Sych’s blog - he covers EVERYTHING. Or you could just go ahead and incorporate my template into your Silverlight project – it works on my machine! 

In order to make the namespace of the generated file match the namespace of your service classes, you need to put this template into a top level folder that matches the name of your service reference. The template will use that directory name to find the edmx file in question:

T4 Template

 

Basically, the template loops over all the entities in the edmx, generates a partial class (which will match the service entities), then loops over all the properties and generated a partial method implementation. Easy?

Download the template file here. You might have to change the extension from txt to tt…