Show TOC

How to Work with AssociationsLocate this document in the navigation structure

This topic gives an overview of Associations.

Context

An association defines the relationship between two entity types, such as, an Employee and Department.

In an application, an instance of an association represents a specific association, such as, an association between an instance of Employee and an instance of Department.

Instances of an association are grouped in an association set. Navigation Properties are special properties on entity types which are bound to a specific association, and can be used to refer to associations of an entity.

There are two ways in which an association can be accessed:

  1. Eager Loading: This allows the user to access the association data inline along with the collection data.
  2. Lazy Loading: This allows the user to access the association when required by explicitly calling it.

To implement the above, proceed as follows:

Procedure

  1. Create a Windows Forms Application.
  2. Delete the automatically created file, App.config in the Solution Explorer.
  3. Generate SAP Service Reference for the service, WFSERVICE, as described in Working with SAP Service Reference to Generate Proxy Classes.
  4. Open the Toolbox.
  5. Drag and drop the DataGridView from the Toolbox to the Windows Forms Application.
  6. Open the class, Form1.cs in View Code mode, from the Start of the navigation path Solution Explorer  Next navigation step  [Project name] End of the navigation path, and use the following code:
    • For Eager Loading: In the example below, use the “Expand DataServiceContext” method to fetch the association data in-line with the collection data.

      You fetch all the tasks using the method Execute, and then fetch the Comments for each task item in-line by using the Expand” method.

      In the “Expand” method, pass the association name (Comments) as the parameter. In case you want to fetch only the main entity data and the associated entity data is not required, use Lazy Loading.

      Note In the example code below, use HTTP or HTTPS according to the setup of your SAP Gateway system.
      The following is a sample code snippet:
      
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Data.Services.Client;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows.Forms;
      
      namespace EagerLoading
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
                  GetDataWithAssociations();
              }
      
              private void GetDataWithAssociations()
              {
                  StringBuilder commentBuilder = new StringBuilder();
                  DataTable taskList;
      		  SAP.IW.GWM.Common.Configuration.ApplicationConfigReader.ConfigFilePath = new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath;
                  WFSERVICE.WFSERVICE serviceContext = new WFSERVICE.WFSERVICE(new Uri("http://hostname:portnumber/sap/opu/odata/opu/WFSERVICE/"));
                  QueryOperationResponse<WFSERVICE.WorkflowTask> serviceresponse = serviceContext.WorkflowTaskCollection.Expand("Comments").Execute() as QueryOperationResponse<WFSERVICE.WorkflowTask>;
                  taskList = new DataTable();
                  taskList.Columns.Add("WorkItem_ID");
                  taskList.Columns.Add("Comment");
                  foreach (var item in serviceresponse)
                  {
                      foreach (var comment in item.Comments)
                      {
                          commentBuilder.Append(comment.text);
                      }
                      taskList.Rows.Add(new object[] { item.workitem_id, commentBuilder.ToString() });
                      commentBuilder.Clear();
                  }
                  dataGridView1.DataSource = taskList;
              }
          }
      }
      
    • For Lazy Loading: In the following example, you use the LoadPropertyDataServiceContext method to fetch the required association for the collection.

      You first fetch all the tasks by using the Execute method and then fetch the Comments for each task item by explicitly calling the LoadProperty method. In the LoadProperty method, you pass the entity object (task item) and the association name (“Comments”) as parameters.

      This means that, to fetch the comments, calls are being made again and again, after all the tasks have been fetched. This could lead to a steep fall in the efficiency of the application.

      Note In the example code below, use HTTP or HTTPS according to the setup of your SAP Gateway system.
      public Form1()
              {
                  InitializeComponent();
                  GetDataWithAssociations();
              }
      
              private void GetDataWithAssociations()
              {
                  StringBuilder commentBuilder = new StringBuilder();
                  DataTable taskList;
      		  SAP.IW.GWM.Common.Configuration.ApplicationConfigReader.ConfigFilePath = new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath;
      
                  WFSERVICE.WFSERVICE serviceContext = new WFSERVICE.WFSERVICE(new Uri("http://<host>:<port>/sap/opu/odata/iwwrk/WFSERVICE/"));
                  QueryOperationResponse<WFSERVICE.WorkflowTask>  serviceresponse = serviceContext.WorkflowTaskCollection.Execute() as QueryOperationResponse<WFSERVICE.WorkflowTask>;
                  
                  taskList = new DataTable();
                  taskList.Columns.Add("WorkItem_ID");
                  taskList.Columns.Add("Comment");
                  
                  foreach (var item in serviceresponse)
                  {
                      serviceContext.LoadProperty(item,"Comments");
                      foreach (var comment in item.Comments)
                      {
                          commentBuilder.Append(comment.text);
                      }
                      taskList.Rows.Add(new object[] { item.workitem_id, commentBuilder.ToString() });
                      commentBuilder.Clear();
                  }
      
                  dataGridView1.DataSource = taskList;
              }
    s

    The result is as shown below:

    Association_Result