通过使用分部方法添加业务逻辑
可以使用分部方法自定义 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 中通常使用这种方式来重写 Insert
、Update
、Delete
的默认方法以及在对象生命周期事件过程中验证属性。
有关详细信息,请参阅分部方法 (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
下面的示例用到了 Shipper
和 Order
实体之间的关系。 请注意这些方法中的分部方法 InsertShipper
和 DeleteShipper
。 这些方法重写了由 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