使用分部方法添加业务逻辑 (LINQ to SQL)

可以通过使用分部方法在 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#)。

示例

说明

下面的示例首先显示了 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 实体之间的关系。 请注意这些方法中的分部方法 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)