# 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** \
&#x20;Please see [Release Notes](https://github.com/OpenRIAServices/OpenRiaServices/releases) or [Changelog](https://github.com/OpenRIAServices/OpenRiaServices/blob/main/Changelog.md) 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. 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](https://openriaservices.gitbook.io/openriaservices/ee707348/ee707373).
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](https://github.com/OpenRIAServices/OpenRiaServices/tree/086ea8c8fcb115000749be6b2b01cd43bb95bf80/docs/ff423126.md) 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](https://github.com/OpenRIAServices/OpenRiaServices/tree/086ea8c8fcb115000749be6b2b01cd43bb95bf80/docs/ff423126.md) 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
   ```

   ```csharp
   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](https://github.com/OpenRIAServices/OpenRiaServices/tree/086ea8c8fcb115000749be6b2b01cd43bb95bf80/docs/ff422141.md) attribute with the [UsingCustomMethod](https://github.com/OpenRIAServices/OpenRiaServices/tree/086ea8c8fcb115000749be6b2b01cd43bb95bf80/docs/ff422690.md) 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
  ```

  ```csharp
  [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](https://github.com/OpenRIAServices/OpenRiaServices/tree/086ea8c8fcb115000749be6b2b01cd43bb95bf80/docs/ff422910.md) method, as shown in the following code.

  ```
  selectedCustomer.ResetPassword()
  customerContext.SubmitChanges(AddressOf OnSubmitCompleted, Nothing)
  ```

  ```csharp
  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](https://github.com/OpenRIAServices/OpenRiaServices/tree/086ea8c8fcb115000749be6b2b01cd43bb95bf80/docs/ff422624.md) 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
  ```

  ```csharp
  [Invoke]
  public int GetLocalTemperature(string postalcode)
  {
      // Implement logic to look up temperature
  }
  ```

  You call the method by using an [InvokeOperation\\](https://github.com/OpenRIAServices/OpenRiaServices/tree/086ea8c8fcb115000749be6b2b01cd43bb95bf80/docs/ff422679.md) object, as shown in the following code.

  ```
  Dim invokeOp As InvokeOperation(Of Integer)
  invokeOp = customerContext.GetLocalTemperature(selectedPostalCode)
  ```

  ```csharp
  InvokeOperation<int> invokeOp = customerContext.GetLocalTemperature(selectedPostalCode);
  ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openriaservices.gitbook.io/openriaservices/ee707348/ee707373/ee796240.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
