How to: Add Business Logic to the Domain Service
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
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.
In the update, insert, or delete methods, add code that specifies the logic for processing the request.
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
RetrieveSalesPersonForCompanymethod 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.
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 following example shows a method that allows a user in the
CustomerRepresentativerole to reset a customer's 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)
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.
You call the method by using an InvokeOperation object, as shown in the following code.
Dim invokeOp As InvokeOperation(Of Integer) invokeOp = customerContext.GetLocalTemperature(selectedPostalCode)