Share via


使用部分方法加入商務邏輯 (LINQ to SQL)

您可以使用「部分方法」(Partial Method),在 LINQ to SQL 專案中自訂 Visual Basic 和 C# 產生的程式碼。 LINQ to SQL 產生的程式碼會將簽章定義成部分方法的一部分。 如果您想實作此方法,可以加入自己的部分方法。 如果您未加入自己的實作 (Implementation),則編譯器 (Compiler) 會捨棄部分方法簽章並呼叫 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) 或 partial (方法) (C#)。

範例

說明

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

程式碼

' 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 實體 (Entity) 之間的關聯性 (Relationship)。 請注意部分方法中的 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)