HOW TO:將商務邏輯加入至網域服務
在本主題中,您將了解如何在 RIA Services 應用程式中加入商務邏輯至網域服務。RIA Services 網域服務預設包含更新、插入和刪除方法,但您通常需要加入其他商務邏輯,才能修改資料。您可能也需要加入執行作業的方法,並不是傳統的查詢、更新、插入或刪除方法。在本主題中,您將學習如何修改資料作業以符合商務需求。您也將學習如何加入具名更新方法和叫用作業。
若要加入商務邏輯至資料修改方法
建立應用程式所需的更新、插入或刪除方法。
在 [加入新的 DomainService 類別] 對話方塊中產生網域服務時選取 [啟用編輯],或加入符合作業之預期簽章的方法,即可建立上述方法。如需詳細資訊,請參閱網域服務。
在更新、插入或刪除方法中加入指定邏輯的程式碼來處理要求。
加入任何符合商務需求的其他必要方法。如果不想要將方法公開為服務,請標記為 IgnoreAttribute 屬性。
下列範例示範一個指派銷售人員 (如果未指派) 的插入方法。如果某公司客戶在資料庫中,
RetrieveSalesPersonForCompany
方法會擷取該公司銷售人員的名稱。這個方法標記著 IgnoreAttribute 屬性,可防止被以服務方式從用戶端呼叫。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; }
若要加入具名更新方法
在 DomainService 類別中加入一個符合具名更新方法之預期簽章的方法。
這個方法必須標記著 UpdateAttribute 屬性 (Attribute) 且 UsingCustomMethod 屬性 (Property) 設為 true,或是不傳回值且接受實體做為第一個參數。
下列範例示範的方法會允許
CustomerRepresentative
角色的使用者重設客戶的密碼。<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 }
加入具名更新方法時,在用戶端專案中會產生兩個方法。一個方法產生在網域內容上,另一個方法產生在當成具名更新方法之參數傳遞的實體上。您可以呼叫網域用戶端上產生的方法或是實體上產生的方法,執行這個具名更新方法。呼叫任一方法後,必須呼叫 SubmitChanges 方法,如下列程式碼所示。
selectedCustomer.ResetPassword() customerContext.SubmitChanges(AddressOf OnSubmitCompleted, Nothing)
selectedCustomer.ResetPassword(); customerContext.SubmitChanges(OnSubmitCompleted, null);
若要加入叫用作業
在 DomainService 類別中加入一個標記著 InvokeAttribute 屬性的方法。
下列範例示範的方法會根據郵遞區號擷取當地溫度。
<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 }
您可以使用 InvokeOperation 物件呼叫該方法,如下列程式碼所示。
Dim invokeOp As InvokeOperation(Of Integer) invokeOp = customerContext.GetLocalTemperature(selectedPostalCode)
InvokeOperation<int> invokeOp = customerContext.GetLocalTemperature(selectedPostalCode);