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

How to: Expose Domain Methods

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 illustrates how to expose stored procedures or functions (domain methods) through ASP.NET Web API.

When a stored procedure or database function is included in the domain model, you could create a function (domain method) for it. Adding a domain method allows you to call the corresponding stored procedure from your code. You could create a domain method that returns a single scalar value, a collection of the Persistent Types, or Complex Types, or no value. For more information, check out the Stored Procedures and Functions section.

Suppose you have a domain model containing a stored procedure named GetCarsDetails. It is mapped to a domain method, which returns a complex type.

Basically, you have to define a new custom action method in your api controller. But before doing this, you will need to extend the corresponding repository class by adding a new method that will call the generated domain method from the context.

Extending the Repository

Suppose that you want to expose the domain method in the CarsController. The first step is to create a new wrapper method in the corresponding repository (e.g. the CarRepository) that will invoke the generated context method. As described in the How to: Extend Web API Controller, you need to add a new partial class with the same name as the name of the repository you want to extend:

public partial class CarRepository
{
   public IEnumerable<CarDetails> GetCarsDetails( string make )
   {
       return this.dataContext.GetCarsDetails( make );
   }
}
Partial Public Class CarRepository
 Public Function GetCarsDetails(ByVal make As String) As IEnumerable(Of CarDetails)
  Return Me.dataContext.GetCarsDetails(make)
 End Function
End Class

Extending the Controller

The second step is to extend the controller (e.g. the CarsController) by adding a new action method. It will simply call the repository method you defined on the previous step.

public partial class CarsController
{
   public virtual IEnumerable<CarDetails> GetCarsDetails( string make )
   {
       return ( this.repository as CarRepository ).GetCarsDetails( make );
   }
}
Partial Public Class CarsController
 Public Overridable Function GetCarsDetails(ByVal make As String) As IEnumerable(Of CarDetails)
  Return (TryCast(Me.repository, CarRepository)).GetCarsDetails(make)
 End Function
End Class