Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
How to: Execute Business Logic When Tracking Changes
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > Working with Objects > How to: Execute Business Logic When Tracking 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.

Telerik OpenAccess ORM enables you to execute your own custom logic and perform custom actions when changes are made to generated properties.

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

To implement custom validation for property changes:

  1.  In your project use the Tracking property of your scope to locate the possible events.
  2. Subscribe the Changing event to a custom method that will perform your validation
    You can subscribe by writing scope.Tracking.Changing+= and double hitting tab after that. Visual studio will automatically creat the subscribition and your custom method. You will only need to replace the "throw new NotImplementedException();" with your logic.

Example:

This example shows how we can easily check if the input value in the Weekly property(or any other property) of the RentalRates class is positive. If the number is not positive we will throw an exception.If the number is positive we will print out the old and the new value of the property in both cases, when is being changed and when is actually changed.

C# Copy Code
class Program
   {
       
static void Main(string[] args)
       {
           IObjectScope scope = OpenAccessData.ObjectScopeProvider1.GetNewObjectScope();
           scope.Tracking.Changing +=
new ChangeEventHandler(Tracking_Changing);
           scope.Tracking.Changed +=
new ChangeEventHandler(Tracking_Changed);
         
           scope.Transaction.Begin();
           var result = from c
in scope.Extent<OpenAccessData.RentalRate>() select c;
           
decimal? myValue = 1;
           
try
           {
               
result.First().Weekly = myValue;
             
           }
           
catch (Exception e)
           {
               Console.WriteLine(e.Message);
              
           }
           scope.Transaction.Commit();
       }
       
static void Tracking_Changed(object sender, ChangeEventArgs e)
       {
           Console.WriteLine(
"Property successfully changed from"+e.OldValue+" to "+e.NewValue);
       }
     
       
static void Tracking_Changing(object sender, ChangeEventArgs e)
       {
           RentalRate rt = (RentalRate)e.PersistentObject;
           
if (e.FieldName.Contains("weekly"))
           {
               
if (Int32.Parse(e.NewValue.ToString()) < 0)
               {
                   
throw new Exception("Number is not possitive");
               }
               
else
               {
                   
Console.WriteLine("Property is being changed from " + e.OldValue + " to " + e.NewValue);
               }
           }          
       }    
   }
VB .net Copy Code
Friend Class Program
  Shared Sub Main(ByVal args() As String)
   Dim scope As IObjectScope = OpenAccessData.ObjectScopeProvider1.GetNewObjectScope()
   AddHandler scope.Tracking.Changing, AddressOf Tracking_Changing
   AddHandler scope.Tracking.Changed, AddressOf Tracking_Changed
   scope.Transaction.Begin()
   Dim result = From c In scope.Extent(Of OpenAccessData.RentalRate)() _
                Select c
   Dim myValue As Nullable(Of Decimal) = 1
   Try
    result.First().Weekly = myValue
   Catch e As Exception
    Console.WriteLine(e.Message)
   End Try
   scope.Transaction.Commit()
  End Sub
  Private Shared Sub Tracking_Changed(ByVal sender As Object, ByVal e As ChangeEventArgs)
   Console.WriteLine("Property successfully changed from" & e.OldValue & " to " & e.NewValue)
  End Sub

  Private Shared Sub Tracking_Changing(ByVal sender As Object, ByVal e As ChangeEventArgs)
   Dim rt As RentalRate = CType(e.PersistentObject, RentalRate)
   If e.FieldName.Contains("weekly") Then
    If Int32.Parse(e.NewValue.ToString()) < 0 Then
     Throw New Exception("Number is not possitive")
    Else
     Console.WriteLine("Property is being changed from " & e.OldValue & " to " & e.NewValue)
    End If
   End If
  End Sub
 End Class

Other possible modifications

For more flexibility you can use the other events in the tracking property of the scope.

Event Description
Changing The changing event occurs when there is a change to an property that is just to be done. You can use this event to check the value that is being passed to certain property and change it if neccessary.
Changed The changed event occurs after the change of given property has been made. You can use this event to reflect the changes to your form.
Adding The adding event occurs just before new object is added. You can use this event to cancel any new entries if they do not mach some criteria.
Added This event occurs right after a new item has been added.
Refreshing This event occurs when the .Refresh() is called, but before the object is refreshed.
Refreshed This event occurs when the object has been refreshed.
Removing This event occurs when an object is to be removed from the scope, but just before the actual removal.
Removed This event occurs when an object has been removed from the scope.