📒
OpenRiaServices
  • Start
  • Prerequisites for Open Ria Services
    • Walkthrough: Installing and Configuring SQL Server 2008 R2 Express with Advanced Services
    • Walkthrough: Installing the AdventureWorks OLTP and LT sample databases
  • Creating Open Ria Services Solutions
    • Walkthrough: Taking a Tour of Open Ria Services
    • Walkthrough: Creating a Open Ria Services Solution
    • Walkthrough: Creating a Open Ria Service with the Code First Approach
    • Walkthrough: Using the Silverlight Business Application Template
    • Walkthrough: Creating a Open Ria Services Class Library
    • Walkthrough: Localizing a Business Application
    • How to: Create a Domain Service that uses POCO-defined Entities
    • How to: Add or Remove a Open Ria Services Link
    • Using the Domain Service Wizard
  • Building Secure Applications with Open Ria Services
  • Deploying and Localizing a Open Ria Services Solutions
    • Troubleshooting the Deployment of a Open Ria Services Solution
    • Troubleshooting the Deployment of a Open Ria Services Solution
    • Walkthrough: Localizing a Business Application
  • Middle Tier
    • Domain Services
      • Walkthrough: Adding Query Methods
      • How to: Add Business Logic to the Domain Service
      • How to: Create a Domain Service that uses POCO-defined Entities
      • How to: Use HTTPS with a Domain Service
    • Data
      • Compositional Hierarchies
      • Presentation Models
      • Inheritance in Data Models
      • Complex Types
      • Shared Entities
      • Walkthrough: Sharing Entities between Multiple Domain Services
      • How to: Add Metadata Classes
      • How to: Validate Data
      • Managing Data Concurrency
    • Shared Code
      • How to: Share Code through Source Files
      • Walkthrough: Creating a Open Ria Services Class Library
  • Silverlight Clients
    • Client Code Generation
    • DomainContext and Operations
    • DomainDataSource
    • Error Handling on the Client
    • Customizing Generated Code
      • How to: Add Computed Properties on the Client
  • Accessing non-Silverlight Clients
    • ASP.NET Clients
    • Walkthrough: Using the Domain Service in ASP.NET Applications
  • Authentication, Roles, and Profiles
    • How to: Enable Authentication in Open Ria Services
    • How to: Enable Roles in Open Ria Services
    • How to: Enable Profiles in Open Ria Services
    • How to: Create a Custom Authorization Attribute
    • Walkthrough: Using Authentication Service with Silverlight Business Application
    • Walkthrough: Using Authentication Service with Silverlight Navigation Application
  • End-to-EndScenarios
    • Walkthrough: Retrieving and Displaying Data From a Domain Service
    • Walkthrough: Editing Data From a Domain Service
    • Walkthrough: Displaying Data in a Silverlight Business Application
    • Walkthrough: Displaying Related Data in a Silverlight Business Application
Powered by GitBook
On this page
  • To add business logic to data modification methods
  • To add a named update method
  • To add an invoke operation

Was this helpful?

  1. Middle Tier
  2. Domain Services

How to: Add Business Logic to the Domain Service

PreviousWalkthrough: Adding Query MethodsNextHow to: Use HTTPS with a Domain Service

Last updated 4 years ago

Was this helpful?

[ This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date Please see or 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 .

  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 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 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 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
    }
    selectedCustomer.ResetPassword()
    customerContext.SubmitChanges(AddressOf OnSubmitCompleted, Nothing)
    selectedCustomer.ResetPassword();
    customerContext.SubmitChanges(OnSubmitCompleted, null);

To add an invoke operation

  • 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
    }
    Dim invokeOp As InvokeOperation(Of Integer)
    invokeOp = customerContext.GetLocalTemperature(selectedPostalCode)
    InvokeOperation<int> invokeOp = customerContext.GetLocalTemperature(selectedPostalCode);

The method should either be marked with the attribute with the property set to true, or not return a value and accept an entity as the first parameter.

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 method, as shown in the following code.

In the domain service class, add a method that is marked with the attribute.

You call the method by using an object, as shown in the following code.

Release Notes
Changelog
Domain Services
IgnoreAttribute
IgnoreAttribute
UpdateAttribute
UsingCustomMethod
SubmitChanges
InvokeAttribute
InvokeOperation\