How to: Add Non-Persistent Properties on the Client
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 describes how to customize the code generated on a WCF Data Services client. In some cases for Data Services, you want to add properties in the client project that are computed from other properties in the domain class. However, you cannot directly customize the generated code because your changes will be overwritten the next time you update your service reference. When you add non-persistent properties, the property only exists in the client project. Additionally, WCF Data provides partial methods that you can use to raise the event that notifies UI elements that the value has changed. The methods are called only when you have created a corresponding partial method.
Adding Non-Persistent Properties
Suppose, you have a Telerik Data Access Domain Model and WCF Data Service in your server project.
To add a non-persistent property:
- In the client project, add a new class with the same name and namespace as the entity proxy class you want to modify.
- Declare the class as partial.
- Add a new property or method that creates a new value based on one or more values in the entity proxy class.
The following example shows how to create a new non-persistent property for the Car entity:
namespace WcfDataNonPersistentProperties.SCRService
{
public partial class Car
{
public string Description
{
get
{
return this.Make + " " + this.Model;
}
}
}
}
Namespace SCRService
Partial Public Class Car
Public ReadOnly Property Description() As String
Get
Return Me.Make & " " & Me.Model
End Get
End Property
End Class
End Namespace
Make sure that your class has the same name and namespaces as the entity proxy class you want to extend, and it is marked with the partial keyword.
Implementing Partial Methods
The WCF Data Services framework generates partial methods for the entity classes. You can use partial methods to raise the event that notifies UI elements that the value of your non-persistent property has changed. For example, implement the On[PersistentProperty]Changed partial method for each property used in computing the new value of the non-persistent property and call the RaisePropertyChanged method to notify the framework that the non-persistent property has changed.
namespace WcfDataNonPersistentProperties.SCRService
{
public partial class Car
{
public string Description
{
get
{
return this.Make + " " + this.Model;
}
}
partial void OnMakeChanged()
{
this.OnPropertyChanged("Description");
}
partial void OnModelChanged()
{
this.OnPropertyChanged("Description");
}
}
}
Namespace SCRService
Partial Public Class Car
Public ReadOnly Property Description() As String
Get
Return Me.Make & " " & Me.Model
End Get
End Property
Private Sub OnMakeChanged()
Me.OnPropertyChanged("Description")
End Sub
Private Sub OnModelChanged()
Me.OnPropertyChanged("Description")
End Sub
End Class
End Namespace
Thus, a change to either the Make or Model will produce a change in the Description.
You can data bind to this non-persistent property with the following code:
<TextBlock Text="{Binding Description, Mode=OneWay}"/>