How to: Add Business Logic to the Domain Service
[ This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date Please see Release Notes or Changelog for a list of changes since WCF RIA Services ]
In this topic, you will learn how to add business logic to a domain service in a Open Ria Services application. A Open Ria Services domain service contains update, insert, and delete methods by default, but you often need to add additional business logic that modifies the data. You may also need to add methods that perform operations that are not the traditional query, update, insert, or delete methods. In this topic, you will learn how to modify data operations to meet business requirements. You will also learn how to add a named update method and an invoke operation.

To add business logic to data modification methods

  1. 1.
    Create the update, insert, or delete methods that you need in your application.
    You create these methods either by selecting Enable editing when generating the domain service in the Add New Domain Service Class dialog box, or by adding the methods that match the expected signature for the operation. For more information, see Domain Services.
  2. 2.
    In the update, insert, or delete methods, add code that specifies the logic for processing the request.
  3. 3.
    Add any additional methods that are required to meet the business requirement. Mark with the IgnoreAttribute attribute if you do not want the method exposed as a service.
    The following example shows an insert method that assigns the salesperson if one is not assigned. The RetrieveSalesPersonForCompany method retrieves the name of the salesperson for a company if a customer from that company is in the database. The method is marked with the IgnoreAttribute attribute to prevent the method from being called as a service from the client.
    1
    Public Sub InsertCustomer(ByVal customer As Customer)
    2
    If (customer.SalesPerson = String.Empty) Then
    3
    customer.SalesPerson = RetrieveSalesPersonForCompany(customer.CompanyName)
    4
    End If
    5
    ​
    6
    If ((customer.EntityState = EntityState.Detached) _
    7
    = False) Then
    8
    Me.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Added)
    9
    Else
    10
    Me.ObjectContext.Customers.AddObject(customer)
    11
    End If
    12
    End Sub
    13
    ​
    14
    <Ignore()> _
    15
    Public Function RetrieveSalesPersonForCompany(ByVal companyname As String) As String
    16
    Dim salesPersonToAssign As String = "unassigned"
    17
    ​
    18
    Dim customers As List(Of Customer)
    19
    customers = GetCustomers().Where(Function(c) c.CompanyName = companyname).ToList()
    20
    ​
    21
    If (customers.Count > 0) Then
    22
    salesPersonToAssign = customers.First().SalesPerson
    23
    End If
    24
    ​
    25
    Return salesPersonToAssign
    26
    End Function
    Copied!
    1
    public void InsertCustomer(Customer customer)
    2
    {
    3
    if (customer.SalesPerson == String.Empty)
    4
    {
    5
    customer.SalesPerson = RetrieveSalesPersonForCompany(customer.CompanyName);
    6
    }
    7
    ​
    8
    if ((customer.EntityState != EntityState.Detached))
    9
    {
    10
    this.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Added);
    11
    }
    12
    else
    13
    {
    14
    this.ObjectContext.Customers.AddObject(customer);
    15
    }
    16
    }
    17
    ​
    18
    [Ignore]
    19
    public string RetrieveSalesPersonForCompany(string companyname)
    20
    {
    21
    string salesPersonToAssign = "unassigned";
    22
    ​
    23
    List<Customer> customers = GetCustomers().Where(c => c.CompanyName == companyname).ToList();
    24
    if (customers.Count > 0)
    25
    {
    26
    salesPersonToAssign = customers.First().SalesPerson;
    27
    }
    28
    ​
    29
    return salesPersonToAssign;
    30
    }
    Copied!

To add a named update method

  • In the domain service class, add a method that matches the expected signature for a named update method.
    The method should either be marked with the UpdateAttribute attribute with the UsingCustomMethod property set to true, or not return a value and accept an entity as the first parameter.
    The following example shows a method that allows a user in the CustomerRepresentative role to reset a customer's password.
    1
    <RequiresRole("CustomerRepresentative")> _
    2
    Public Sub ResetPassword(ByVal customer As Customer)
    3
    ' Implement logic to reset password
    4
    End Sub
    Copied!
    1
    [RequiresRole("CustomerRepresentative")]
    2
    public void ResetPassword(Customer customer)
    3
    {
    4
    // Implement logic to reset password
    5
    }
    Copied!
    When you add a named update method, two methods are generated in the client project. One method is generated on the domain context and one method is generated on the entity that is passed as a parameter for the named update method. You execute this named update method from the client by calling either the generated method on the domain client or the generated method on the entity. After calling either method, you must call the SubmitChanges method, as shown in the following code.
    1
    selectedCustomer.ResetPassword()
    2
    customerContext.SubmitChanges(AddressOf OnSubmitCompleted, Nothing)
    Copied!
    1
    selectedCustomer.ResetPassword();
    2
    customerContext.SubmitChanges(OnSubmitCompleted, null);
    Copied!

To add an invoke operation

  • In the domain service class, add a method that is marked with the InvokeAttribute attribute.
    The following example shows a method that retrieves the local temperature based on postal code.
    1
    <Invoke()> _
    2
    Public Function GetLocalTemperature(ByVal postalcode As String) As Integer
    3
    ' Implement logic to look up temperature
    4
    End Function
    Copied!
    1
    [Invoke]
    2
    public int GetLocalTemperature(string postalcode)
    3
    {
    4
    // Implement logic to look up temperature
    5
    }
    Copied!
    You call the method by using an InvokeOperation\ object, as shown in the following code.
    1
    Dim invokeOp As InvokeOperation(Of Integer)
    2
    invokeOp = customerContext.GetLocalTemperature(selectedPostalCode)
    Copied!
    1
    InvokeOperation<int> invokeOp = customerContext.GetLocalTemperature(selectedPostalCode);
    Copied!
Last modified 6mo ago