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

How to: Extend Domain Service

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 extend the generated WCF RIA Domain Service class.

Using Partial Classes

When working with automatically generated source (like the domain class generated by the RIA Wizard), code can be added to the class without having to modify the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code and so on. You can create code that uses these classes without having to edit the file created by Visual Studio. Using partial classes is a perfect solution for separating auto-generated code from developer code. The most compelling reason for using a partial class is to separate your application business logic from the designer-generated code.

You may ask yourself "Why do I need to use partial classes?". You could try the following experiment. Modify the domain service class by adding a new dummy method to the class definition. After that, open your DomainService.tt file and save it (Ctrl+S). Now take a look at the class again. The method you just added is gone and the original definition of the class has been restored.

Do not modify the original source file of a domain service class. Each time you re-save your .tt file, all changes will be lost. Instead, add your custom methods in a partial class.

Basically, what you need to do is:

  1. Create a new class with the same name as the name of the domain service you want to extend.
  2. The new partial class should be in the same namespace as the original.
  3. Mark the class with the partial keyword.
  4. Add your custom methods.
public partial class DomainService
{
   [Query( IsComposable = false )]
   public Customer GetCustomerById( int customerId )
   {
       return this.DataContext.Customers.SingleOrDefault( c => c.CustomerID == customerId );
   }
}
Partial Public Class DomainService
 <Query(IsComposable := False)>
 Public Function GetCustomerById(ByVal customerId As Integer) As Customer
  Return Me.DataContext.Customers.SingleOrDefault(Function(c) c.CustomerID = customerId)
 End Function
End Class