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

How to: Handle Relationships

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 will give you general guidelines about working with relationships in RESTful Collection services.

By default, RESTful Collection services limit the amount of data returned by a query. However, you can explicitly load related entities from the service when it is needed. This section describes how to load such deferred content into your application.

When you execute a read operation, only entities in the addressed entity set are returned. For example, in the following demo code only Product entities are returned. By default the related Category entity is not returned, even though there is a relationship between Product and Category.

To request that the call to the service returns entities that are related by an association, you need to modify the code generated by the Service Wizard on the server. The key classes are the assembler classes. The assembler classes provide factory methods for creating DTOs from domain classes and vice versa. In this example, you need to extend the ProductAssembler so that it returns the related category.

Create a new partial class with the same name as the assembler you want to extend, e.g. ProductAssembler.

public partial class ProductAssembler : ProductAssemblerBase, IProductAssembler
{
}
Partial Public Class ProductAssembler
 Inherits ProductAssemblerBase
 Implements IProductAssembler
End Class

Override the Assembly(Entity entity) method and initialize the navigation property in the following manner:

using WcfRestRelationships.Web.Assemblers;
using WcfRestRelationships.Web.Dto;
namespace WcfRestRelationships.Web
{
   public partial class ProductAssembler : ProductAssemblerBase, IProductAssembler
   {
       public override ProductDto Assemble(Product entity)
       {
           ProductDto productDto = base.Assemble(entity);
           if (entity.Category != null)
           {
               productDto.Category = new CategoryAssembler().Assemble(entity.Category);
           }
           return productDto;
       }
   }
}
Imports WcfRestRelationships.Web.Assemblers
Imports WcfRestRelationships.Web.Dto
Partial Public Class ProductAssembler
    Inherits ProductAssemblerBase
    Implements IProductAssembler
    Public Overrides Function Assemble(ByVal entity As Product) As ProductDto
        Dim _productDto As ProductDto = MyBase.Assemble(entity)
        If entity.Category IsNot Nothing Then
            _productDto.Category = New CategoryAssembler().Assemble(entity.Category)
        End If
        Return _productDto
    End Function
End Class

Now, when you execute a read operation, the related category will be returned.

Creating Entities with Relationships

When you add a new entity, any relationships between the new entity and related entities are not automatically defined. In this case you need to override the Assembly(Entity entity, DTO dto) method:

using WcfRestRelationships.Web.Assemblers;
using WcfRestRelationships.Web.Dto;
namespace WcfRestRelationships.Web
{
   public partial class ProductAssembler : ProductAssemblerBase, IProductAssembler
   {
       public override ProductDto Assemble(Product entity)
       {
           ProductDto productDto = base.Assemble(entity);
           if (entity.Category != null)
           {
               productDto.Category = new CategoryAssembler().Assemble(entity.Category);
           }
           return productDto;
       }

       public override Product Assemble(Product entity, ProductDto dto)
       {
           Product product = base.Assemble(entity, dto);
           if (dto != null && dto.Category != null)
           {
               product.Category = new CategoryAssembler().Assemble(null, dto.Category);
           }
           return product;
       }
   }
}
Imports WcfRestRelationships.Web.Assemblers
Imports WcfRestRelationships.Web.Dto
Partial Public Class ProductAssembler
    Inherits ProductAssemblerBase
    Implements IProductAssembler
    Public Overrides Function Assemble(ByVal entity As Product) As ProductDto
        Dim _productDto As ProductDto = MyBase.Assemble(entity)
        If entity.Category IsNot Nothing Then
            _productDto.Category = New CategoryAssembler().Assemble(entity.Category)
        End If
        Return _productDto
    End Function
    Public Overrides Function Assemble(ByVal entity As Product, ByVal dto As ProductDto) As Product
        Dim _product As Product = MyBase.Assemble(entity, dto)
        If dto IsNot Nothing AndAlso dto.Category IsNot Nothing Then
            _product.Category = New CategoryAssembler().Assemble(Nothing, dto.Category)
        End If
        Return _product
    End Function
End Class