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 WCF Plain services.

By default, WCF Plain Services limit the amount of returned data. 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 ReadEntityAsync method, only entities in the addressed entity set are returned. For example, the ReadProductsAsync method returns only Product entities, by default the related Category entity is not returned, even though there is a relationship between Product and Category.

private void Load()
{
   EntitiesModelServiceClient client = new EntitiesModelServiceClient();
   client.ReadProductsCompleted += 
        new EventHandler<ReadProductsCompletedEventArgs>(client_ReadProductsCompleted);
   client.ReadProductsAsync();
}
private void client_ReadProductsCompleted(object sender, ReadProductsCompletedEventArgs e)
{
}
Private Sub Load()
 Dim client As New EntitiesModelServiceClient()
 AddHandler client.ReadProductsCompleted, _
        AddressOf client_ReadProductsCompleted
 client.ReadProductsAsync()
End Sub
Private Sub client_ReadProductsCompleted(ByVal sender As Object, _
    ByVal e As ReadProductsCompletedEventArgs)
End Sub

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 Assemble(Entity entity) method and initialize the navigation property in the following manner:

using WcfPlainRelationships.Web.Assemblers;
using WcfPlainRelationships.Web.Dto;
namespace WcfPlainRelationships.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 WcfPlainRelationships.Web.Assemblers
Imports WcfPlainRelationships.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

Creating Entities with Relationships

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

using WcfPlainRelationships.Web.Assemblers;
using WcfPlainRelationships.Web.Dto;
namespace WcfPlainRelationships.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.CategoryID = dto.Category.CategoryID;
           }
           return product;
       }
   }
}
Imports WcfPlainRelationships.Web.Assemblers
Imports WcfPlainRelationships.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.CategoryID = dto.Category.CategoryID
        End If
        Return _product
    End Function
End Class