Share via


부분 메서드를 사용하여 비즈니스 논리 추가(LINQ to SQL)

부분 메서드(Partial Method)를 사용하여 LINQ to SQL 프로젝트에 Visual Basic 및 C#에서 생성된 코드를 사용자 지정할 수 있습니다. LINQ to SQL에서 생성한 코드에는 부분 메서드의 일부로 시그니처를 정의합니다. 메서드를 구현하려면 사용자 고유의 부분 메서드를 추가합니다. 사용자 고유의 구현을 추가하지 않으면 컴파일러에서는 부분 메서드 시그니처를 취소하고 LINQ to SQL의 기본 메서드를 호출합니다.

참고참고

Visual Studio를 사용하면 개체 관계형 디자이너를 사용하여 엔터티 클래스에 유효성 검사와 기타 사용자 지정을 추가할 수 있습니다.

예를 들어 Northwind 샘플 데이터베이스의 Customer 클래스에 대한 기본 매핑에는 다음 부분 메서드가 포함됩니다.

Partial Private Sub OnAddressChanged()
End Sub
partial void OnAddressChanged();

다음과 같은 코드를 사용자 고유의 부분 Customer 클래스에 추가하여 사용자 고유의 메서드를 구현할 수 있습니다.

Partial Class Customer
    Private Sub OnAddressChanged()
        ' Insert business logic here.
    End Sub
End Class
public partial class Customer
{
    partial void OnAddressChanged();
    partial void OnAddressChanged()
    {
        // Insert business logic here.
    }
}

이러한 접근 방식은 일반적으로 LINQ to SQL에서 Insert, Update 및 Delete에 대한 기본 메서드를 재정의하고 개체 수명 주기 이벤트 동안 속성의 유효성을 검사하는 데 사용됩니다.

자세한 내용은 부분 메서드(Visual Basic)(Visual Basic) 또는 부분(메서드)(C#)을 참조하십시오.

예제

설명

다음 예제에서는 SQLMetal와 같은 코드 생성 도구에서 정의한 ExampleClass를 먼저 보여 준 다음 두 개의 메서드 중 하나만 구현하는 방법을 보여 줍니다.

코드

' Code-generating tool defines a partial class, including 
' two partial methods. 
Partial Class ExampleClass
    Partial Private Sub OnFindingMaxOutput()
    End Sub

    Partial Private Sub OnFindingMinOutput()
    End Sub

    Sub ExportResults()
        OnFindingMaxOutput()
        OnFindingMinOutput()
    End Sub
End Class

' Developer implements one of the partial methods. Compiler
' discards the other method.
Class ExampleClass
    Private Sub OnFindingMaxOutput()
        Console.WriteLine("Maximum has been found.")
    End Sub
End Class
// Code-generating tool defines a partial class, including
// two partial methods.
partial class ExampleClass
{
    partial void onFindingMaxOutput();
    partial void onFindingMinOutput();
}

// Developer implements one of the partial methods. Compiler
// discards the signature of the other method.
partial class ExampleClass
{
    partial void onFindingMaxOutput()
    {
        Console.WriteLine("Maximum has been found.");
    }
}

예제

설명

다음 예제에서는 Shipper와 Order 엔터티 간의 관계를 사용합니다. 메서드 중 부분 메서드는 InsertShipper와 DeleteShipper입니다. 이러한 메서드는 LINQ to SQL 매핑을 통해 제공된 기본 부분 메서드를 재정의합니다.

코드

Public Shared LoadOrdersCalled As Integer = 0
Private Function LoadOrders(ByVal shipper As Shipper) As  _
    IEnumerable(Of Order)
    LoadOrdersCalled += 1
    Return Me.Orders.Where(Function(o) o.ShipVia = _
        shipper.ShipperID)
End Function

Public Shared LoadShipperCalled As Integer = 0
Private Function LoadShipper(ByVal order As Order) As Shipper
    LoadShipperCalled += 1
    Return Me.Shippers.Single(Function(s) s.ShipperID = _
        order.ShipVia)
End Function

Public Shared InsertShipperCalled As Integer = 0
Private Sub InsertShipper(ByVal instance As Shipper)
    InsertShipperCalled += 1
    ' Call a Web service to perform an insert operation.
    InsertShipperService(shpr:=Nothing)
End Sub

Public Shared UpdateShipperCalled As Integer = 0
Private Sub UpdateShipper(ByVal original As Shipper, ByVal current _
    As Shipper)
    UpdateShipperCalled += 1
    ' Call a Web service to update shipper.
    InsertShipperService(shpr:=Nothing)
End Sub

Public Shared DeleteShipperCalled As Boolean
Private Sub DeleteShipper(ByVal instance As Shipper)
    DeleteShipperCalled = True
End Sub
public static int LoadOrdersCalled = 0;
private IEnumerable<Order> LoadOrders(Shipper shipper)
{
    LoadOrdersCalled++;
    return this.Orders.Where(o => o.ShipVia == shipper.ShipperID);
}

public static int LoadShipperCalled = 0;
private Shipper LoadShipper(Order order)
{
    LoadShipperCalled++;
    return this.Shippers.Single(s => s.ShipperID == order.ShipVia);
}

public static int InsertShipperCalled = 0;
partial void InsertShipper(Shipper shipper)
{
    InsertShipperCalled++;
    // Call a Web service to perform an insert operation.
    InsertShipperService(shipper);
}

public static int UpdateShipperCalled = 0;
private void UpdateShipper(Shipper original, Shipper current)
{
    Shipper shipper = new Shipper();
    UpdateShipperCalled++;
    // Call a Web service to update shipper.
    InsertShipperService(shipper);
}

public static bool DeleteShipperCalled;
partial void DeleteShipper(Shipper shipper)
{
    DeleteShipperCalled = true;
}

참고 항목

기타 리소스

데이터 변경 및 변경 내용 전송(LINQ to SQL)

삽입, 업데이트 및 삭제 작업 사용자 지정(LINQ to SQL)