How to: Add Non-Perisistent 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 RIA Services client. In some cases for RIA 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 your project is compiled. When you add non-persistent properties, the property only exists in the client project. Additionally, WCF RIA 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 Telerik Data Access Domain 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 WcfRiaNonPersistentProperties.Web
{
public partial class Car
{
public string Description
{
get
{
return this.Make + " " + this.Model;
}
}
}
}
Namespace Web
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 RIA 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 WcfRiaNonPersistentProperties.Web
{
public partial class Car
{
public string Description
{
get
{
return this.Make + " " + this.Model;
}
}
partial void OnMakeChanged()
{
this.RaisePropertyChanged("Description");
}
partial void OnModelChanged()
{
this.RaisePropertyChanged("Description");
}
}
}
Namespace Web
Partial Public Class Car
Public ReadOnly Property Description() As String
Get
Return Me.Make & " " & Me.Model
End Get
End Property
Private Sub OnMakeChanged()
Me.RaisePropertyChanged("Description")
End Sub
Private Sub OnModelChanged()
Me.RaisePropertyChanged("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}"/>