How to: Implement INotifyPropertyChanging/ed Interface on DTOs (VB)
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
This topic illustrates how to modify the code generation templates used by the Service Wizard, in order to automatically implement the INotifyPropertyChanging/ed interfaces for the generated C# Data Transfer Objects (DTOs).
This topic applies to VB only. For C# version, please refer to How to: Implement INotifyPropertyChanging/ed Interface on DTOs (C#).
To implement INotifyPropertyChanging/ed interfaces:
- Create a new folder named OpenAccessTemplates in the host project, as it is described in the How to: Customize Code Generation topic. The Service Wizard will start looking for code generation templates from the host project and then from the Telerik Data Access installation folder.
-
Copy/Paste the entire <install-dir>/dsl2012/CodeGenerationTemplates/Dto/VisualBasic/Includes folder in the OpenAccessTemplates folder:
-
Open the General.ttinclude template. The INotifyPropertyChanging/ed interfaces are in the System.ComponentModel namespace. By default that namespace is not included in the generated DTOs. The first step is to include it. Navigate to the GenerateNamespaces method (in the original version of the template, the beginning of the method should be located at line 165). Add the System.ComponentModel namespace like the code snippet below:
this.WriteLine("Imports System.ComponentModel");
-
You need to modify the Specific.ttinclude template and add code for generation of the INotifyPropertyChanged/Changing events and methods that raise the events. Open the Specific.ttinclude template. The first step is to change the class signature so that it implements the INotifyPropertyChanged/Changing interfaces. Navigate to line 67 or find the following code:
Partial Public Class <#= this.nameService.GetDTOName(@class.Name) #> Implements <#= this.nameService.DtoWithKeyInterfaceName #>
Modify this line so it looks like this:
Partial Public Class <#= this.nameService.GetDTOName(@class.Name) #> Implements <#= this.nameService.DtoWithKeyInterfaceName #> Implements INotifyPropertyChanged Implements INotifyPropertyChanging
-
Next, you need to add code for generation of the INotifyPropertyChanged/Changing events and methods that raise the events. Go to line 100-101 and find the following code:
<#+ this.GenerateProperties(@class.Properties); #> End Class
Add the following code between the "#>" and "EndClass" statements:
<#+ this.GenerateProperties(@class.Properties); #> Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) _ Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged Public Event PropertyChanging(sender As Object, e As System.ComponentModel.PropertyChangingEventArgs) _ Implements System.ComponentModel.INotifyPropertyChanging.PropertyChanging Protected Overridable Sub OnPropertyChanged(ByVal info As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info)) End Sub Protected Overridable Sub OnPropertyChanging(ByVal info As String) RaiseEvent PropertyChanging(Me, New PropertyChangingEventArgs(info)) End Sub End Class
-
The final step is to modify the DTO properties to provide change notifications. Navigate to the GenerateProperty(PropertyDetail details) method and find the code for set generation:
Set Me.<#= details.fieldName #> = value End Set
Modify it so it looks like this:
Set Me.OnPropertyChanging("<#= details.propertyName #>") Me.<#= details.fieldName #> = value Me.OnPropertyChanged("<#= details.propertyName #>") End Set
Save your templates and generate your WCF Service. Now all DTO classes from the Transport.vb file should implement the INotifyPropertyChanged/Changing interfaces.