Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
How to: Execute Business Logic During Property Changes
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > Working with Objects > How to: Execute Business Logic During Property Changes

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

In this task you will learn how to execute business logic when saving changes using the INotifyPropertyChanged and INotifyPropertyChanging interfaces.

The example in this task is based on the Northwind database.To run the code in this example, you must have already added the Northwind data Model to your project and configured your project to use the OpenAccess Framework.

To implement custom methods for property changes:

  1. In the you want to implement the custom methods, inherit both INotifyPropertyChanged and INotifyPropertyChanging.
    INotifyPropertyChanging is available in .net 3.5 framework only.
  2. Implement the interfaces of both.
  3. Create private virtual void methods called OnPropertyChanged and OnPropertyChanging where your custom logic will be.

Example:

 This example shows how to implement custom methods in the Category class. Those methods will notify us whenever a property is being changed or it has been changed.

C# Copy Code
public partial class Category : INotifyPropertyChanging, INotifyPropertyChanged
   {
       
//The 'no-args' constructor required by OpenAccess.
       
public Category()
       {
       }
       
//INotifyPropertyChanging Members
       
public event PropertyChangingEventHandler PropertyChanging;
       
protected virtual void OnPropertyChanging(string propertyName)
       {
           
if (this.PropertyChanging != null)
               
this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
           Console.WriteLine(propertyName +
" is being changed");
       }

       
//INotifyPropertyChanged Members
       
public event PropertyChangedEventHandler PropertyChanged;
       
protected virtual void OnPropertyChanged(string propertyName)
       {
           
if (this.PropertyChanged != null)
               
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
           Console.WriteLine(propertyName +
" has changed");
       }
       [Telerik.OpenAccess.FieldAlias(
"categoryID")]
       
public int CategoryID
       {
           get {
return categoryID; }
           set
           {
               OnPropertyChanging(
"CategoryID");
               
this.categoryID = value;
               OnPropertyChanged(
"CategoryID");
           }
       }
       [Telerik.OpenAccess.FieldAlias(
"categoryName")]
       
public string CategoryName
       {
           get {
return categoryName; }
           set
           {
               OnPropertyChanging(
"CategoryName");
               
this.categoryName = value;
               OnPropertyChanged(
"CategoryName");
           }
       }
       [Telerik.OpenAccess.FieldAlias(
"description")]
       
public string Description
       {
           get {
return description; }
           set
           {
               OnPropertyChanging(
"Description");
               
this.description = value;
               OnPropertyChanged(
"Description");
           }
       }
       [Telerik.OpenAccess.FieldAlias(
"picture")]
       
public byte[] Picture
       {
           get {
return picture; }
           set
           {
               OnPropertyChanging(
"Picture");
               
this.picture = value;
               OnPropertyChanged(
"Picture");
           }
       }

   }
VB.NET Copy Code
Partial Public Class Category
    Implements INotifyPropertyChanging, INotifyPropertyChanged
    'The 'no-args' constructor required by OpenAccess.
    Public Sub New()
    End Sub
    'INotifyPropertyChanging Members
    Public Event PropertyChanging(ByVal sender As Object, ByVal e As PropertyChangingEventArgs) Implements INotifyPropertyChanging.PropertyChanging
    Protected Overridable Sub OnPropertyChanging(ByVal propertyName As String)
        If Me.PropertyChangingEvent IsNot Nothing Then
            RaiseEvent PropertyChanging(Me, New PropertyChangingEventArgs(propertyName))
            Console.WriteLine(propertyName & " is being changed")
        End If
    End Sub
    'INotifyPropertyChanged Members
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
    'Custom method that will be called when a property is about to change
    Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
        If Me.PropertyChangedEvent IsNot Nothing Then
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
            Console.WriteLine(propertyName & " has changed")
        End If
    End Sub
    <Telerik.OpenAccess.FieldAlias("_categoryID")> _
    Public Property CategoryID() As Integer
        Get
            Return _categoryID
        End Get
        Set( ByVal Value As Integer )
            OnPropertyChanging("CategoryID")
            Me._categoryID = Value
            OnPropertyChanged("CategoryID")
        End Set
    End Property

    <Telerik.OpenAccess.FieldAlias("_categoryName")> _
    Public Property CategoryName() As String
        Get
            Return _categoryName
        End Get
        Set( ByVal Value As String )
            OnPropertyChanging("CategoryName")
            Me._categoryName = Value
            OnPropertyChanged("CategoryName")
        End Set
    End Property

    <Telerik.OpenAccess.FieldAlias("_description")> _
    Public Property Description() As String
        Get
            Return _description
        End Get
        Set( ByVal Value As String )
            OnPropertyChanging("Description")
            Me._description = Value
            OnPropertyChanged("Description")
        End Set
    End Property

    <Telerik.OpenAccess.FieldAlias("_picture")> _
    Public Property Picture() As Byte()
        Get
            Return _picture
        End Get
        Set( ByVal Value As Byte() )
            OnPropertyChanging("Picture")
            Me._picture = Value
            OnPropertyChanged("Picture")
        End Set
    End Property

End Class