다음을 통해 공유


방법: 도메인 서비스 비즈니스 논리 추가

이 항목에서는 RIA Services 응용 프로그램의 도메인 서비스에 비즈니스 논리를 추가하는 방법을 배웁니다. RIA Services 도메인 서비스에는 기본적으로 update, insert 및 delete 메서드가 포함되어 있지만 데이터를 수정하는 추가 비즈니스 논리를 추가해야 하는 경우도 있습니다. 기존 query, update, insert 또는 delete 메서드가 아닌 작업을 수행하는 메서드를 추가해야 할 수도 있습니다. 이 항목에서는 비즈니스 요구 사항을 충족하도록 데이터 작업을 수정하는 방법과 명명된 업데이트 메서드 및 호출 작업을 추가하는 방법을 배웁니다.

데이터 수정 메서드에 비즈니스 논리를 추가하려면

  1. 응용 프로그램에서 필요한 update, insert 또는 delete 메서드를 만듭니다.

    편집 사용(새 도메인 서비스 클래스 추가 대화 상자에서 도메인 서비스를 생성하는 경우)을 선택하거나 작업에 필요한 시그니처와 일치하는 메서드를 추가하여 이러한 메서드를 만듭니다. 자세한 내용은 도메인 서비스를 참조하십시오.

  2. update, insert 또는 delete 메서드에서 요청을 처리하는 논리를 지정하는 코드를 추가합니다.

  3. 비즈니스 요구 사항을 충족하는 데 필요한 메서드를 더 추가합니다. 메서드를 서비스로 노출하지 않으려면 IgnoreAttribute 특성으로 표시합니다.

    다음 예제에서는 판매 직원(할당되지 않은 경우)을 할당하는 insert 메서드를 보여 줍니다. 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;
    }
    

명명된 업데이트 메서드를 추가하려면

  1. 도메인 서비스 클래스에서 명명된 업데이트 메서드에 필요한 시그니처와 일치하는 메서드를 추가합니다.

    메서드는 UsingCustomMethod 속성이 true로 설정된 UpdateAttribute 특성으로 표시되거나, 값을 반환하지 않고 엔터티를 첫 번째 매개 변수로 받아들여야 합니다.

    다음 예제에서는 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);
    

호출 작업을 추가하려면

  1. 도메인 서비스 클래스에서 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);