How to: Add Business Logic to the Domain Service

[WCF RIA Services Version 1 Service Pack 2 is compatible with either .NET framework 4 or .NET Framework 4.5, and with either Silverlight 4 or Silverlight 5.]

In this topic, you will learn how to add business logic to a domain service in a RIA Services application. A 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. 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. In the update, insert, or delete methods, add code that specifies the logic for processing the request.

  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.

    Public Sub InsertCustomer(ByVal customer As Customer)
        If (customer.SalesPerson = String.Empty) Then
            customer.SalesPerson = RetrieveSalesPersonForCompany(customer.CompanyName)
        End If
    
        If ((customer.EntityState = EntityState.Detached) _
                    = False) Then
            Me.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Added)
        Else
            Me.ObjectContext.Customers.AddObject(customer)
        End If
    End Sub
    
    <Ignore()> _
    Public Function RetrieveSalesPersonForCompany(ByVal companyname As String) As String
        Dim salesPersonToAssign As String = "unassigned"
    
        Dim customers As List(Of Customer)
        customers = GetCustomers().Where(Function(c) c.CompanyName = companyname).ToList()
    
        If (customers.Count > 0) Then
            salesPersonToAssign = customers.First().SalesPerson
        End If
    
        Return salesPersonToAssign
    End Function
    
    public void InsertCustomer(Customer customer)
    {
        if (customer.SalesPerson == String.Empty)
        {
            customer.SalesPerson = RetrieveSalesPersonForCompany(customer.CompanyName);
        }
    
        if ((customer.EntityState != EntityState.Detached))
        {
            this.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Added);
        }
        else
        {
            this.ObjectContext.Customers.AddObject(customer);
        }
    }
    
    [Ignore]
    public string RetrieveSalesPersonForCompany(string companyname)
    {
        string salesPersonToAssign = "unassigned";
    
        List<Customer> customers = GetCustomers().Where(c => c.CompanyName == companyname).ToList();
        if (customers.Count > 0)
        {
            salesPersonToAssign = customers.First().SalesPerson;
        }
    
        return salesPersonToAssign;
    }
    

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.

    <RequiresRole("CustomerRepresentative")> _
    Public Sub ResetPassword(ByVal customer As Customer)
        ' Implement logic to reset password
    End Sub
    
    [RequiresRole("CustomerRepresentative")]
    public void ResetPassword(Customer customer)
    {
        // Implement logic to reset password
    }
    

    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.

    selectedCustomer.ResetPassword()
    customerContext.SubmitChanges(AddressOf OnSubmitCompleted, Nothing)
    
    selectedCustomer.ResetPassword();
    customerContext.SubmitChanges(OnSubmitCompleted, null);
    

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.

    <Invoke()> _
    Public Function GetLocalTemperature(ByVal postalcode As String) As Integer
        ' Implement logic to look up temperature
    End Function
    
    [Invoke]
    public int GetLocalTemperature(string postalcode)
    {
        // Implement logic to look up temperature
    }
    

    You call the method by using an InvokeOperation<TValue> object, as shown in the following code.

    Dim invokeOp As InvokeOperation(Of Integer)
    invokeOp = customerContext.GetLocalTemperature(selectedPostalCode)
    
    InvokeOperation<int> invokeOp = customerContext.GetLocalTemperature(selectedPostalCode);