共用方式為


使用部分方法加入商務邏輯

您可以使用「部分方法」在 LINQ to SQL 專案中自訂 Visual Basic 和 C# 產生的程式碼。 LINQ to SQL 產生的程式碼會將簽章定義成部分方法的一部分。 如果您想實作此方法,可以加入自己的部分方法。 如果不加入自己實作的部分,編譯器會捨棄部分方法的簽章,並呼叫 LINQ to SQL 的預設方法。

注意

如果您使用 Visual Studio,您可以使用物件關聯式設計工具,將驗證和其他自訂項目新增到實體類別。

例如,Northwind 範例資料庫中 Customer 類別的預設對應包含下列部分方法:

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

您可以將下列程式碼加入至自己的部分 Customer 類別,進而實作自己的方法:

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

這種方法通常用於 LINQ to SQL 中,藉此覆寫 InsertUpdateDelete 的預設方法,以及驗證物件生命週期事件中的屬性。

如需詳細資訊,請參閱部分方法 (Visual Basic) 或部分 (方法) (C# 參考) (C#)。

範例 1

下列範例會先顯示 ExampleClass,因為它可能是由程式碼產生工具 (例如 SQLMetal) 所定義,然後顯示您如何僅實作其中一種方法。

// 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.");
    }
}
' 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

範例 2

下列範例會使用 ShipperOrder 實體 (Entity) 之間的關聯性 (Relationship)。 請注意部分方法中的 InsertShipperDeleteShipper 方法。 這些方法會覆寫 LINQ to SQL 對應所提供的預設部分方法。

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;
}
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

另請參閱