Data Access has been discontinued. Please refer to this page for more information.

How to: Validate Data

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.

WCF Data Services enables an application to intercept request messages so that you can add custom logic to an application. You can use this custom logic to validate data in incoming messages. You can also use it to further restrict the scope of a query request, such as to insert a custom authorization policy on a per request basis.

Interception is performed by specially attributed methods in the data service. These methods are called by the ADO.NET Data Services at the appropriate point in message processing. Interceptors cannot accept parameters. Query interceptor methods, which are called when processing an HTTP GET request, must return a lambda expression that is used by the data service to further refine the requested operation. Change interceptors on the other hand are invoked when the data is returned for processing on the server. They must return void (Nothing in Visual Basic).

This task defines QueryInterceptor and ChangeInterceptor, respectively for the Car object.

Defining QueryInterceptor

To define a query interceptor for the Car object:

  1. In the Web project open the SofiaCarRentalWCFDataService.svc.cs file. The SofiaCarRentalWCFDataService was defined in the Walkthrough: Creating a WCF Data Services Solution by using the Service Wizard.
  2. Define a service operation method, named OnQueryCars() as follows:

    [QueryInterceptor( "Cars" )]
    public Expression<Func<Car, bool>> OnQueryCars()
    {
       return o => o.CarYear == 2001;
    }
    
    <QueryInterceptor("Cars")>
    Public Function OnQueryCars() As Expression(Of Func(Of Car, Boolean))
     Return Function(o) o.CarYear = 2001
    End Function
    

    This will filter the Cars additionally and will return only those Cars, which CarYear is equal to 2001. In more details, this example defines a query interceptor method for the Car objects that returns a lambda expression. This expression contains a delegate that filters the requested Cars based on a specific CarYear.

Defining ChangeInterceptor

To define a change interceptor for the OrderDetail objects:

  1. In the Web project open the SofiaCarRentalWCFDataService.svc.cs file. The SofiaCarRentalWCFDataService was defined in the Walkthrough: Creating a WCF Data Services Solution by using the Service Wizard.
  2. Define a service operation method, named OnChangeCars as follows:

    [ChangeInterceptor( "Cars" )]
    public void OnChangeCars( Car car, UpdateOperations operations )
    {
       if ( operations == UpdateOperations.Add ||
           operations == UpdateOperations.Change )
       {
           if ( car.Make == "VW" )
               throw new DataServiceException( 400,
                   "A car with make equals to VW, cannot be modified" );
       }
       else if ( operations == UpdateOperations.Delete )
       {
           throw new DataServiceException( 400,
               "Cars cannot be deleted" );
       }
    }
    
    <ChangeInterceptor("Cars")>
    Public Sub OnChangeCars(ByVal car_Renamed As Car, ByVal operations As UpdateOperations)
     If operations = UpdateOperations.Add OrElse operations = UpdateOperations.Change Then
      If car_Renamed.Make = "VW" Then
       Throw New DataServiceException(400, "A car with make equals to VW, cannot be modified")
      End If
     ElseIf operations = UpdateOperations.Delete Then
      Throw New DataServiceException(400, "Cars cannot be deleted")
     End If
    End Sub
    

    This example defines a change interceptor method for the Cars. This method validates input to the service from an Add or Change operation and raises an exception if a change is being made on a Car with Make equals to VW. It also blocks the deletion of Cars as an unsupported operation.