Aggiunta di logica di business mediante metodi parzialiAdding Business Logic By Using Partial Methods

È possibile personalizzare Visual Basic e c# generato codice il LINQ to SQLLINQ to SQL progetti utilizzando metodi parziali.You can customize Visual Basic and C# generated code in your LINQ to SQLLINQ to SQL projects by using partial methods. Il codice generato da LINQ to SQLLINQ to SQL definisce le firme come una parte di un metodo parziale.The code that LINQ to SQLLINQ to SQL generates defines signatures as one part of a partial method. Se si desidera implementare il metodo, è possibile aggiungere un metodo parziale personalizzato.If you want to implement the method, you can add your own partial method. Se non si aggiunge un'implementazione personalizzata, il compilatore ignora la firma dei metodi parziali e chiama i metodi predefiniti in LINQ to SQLLINQ to SQL.If you do not add your own implementation, the compiler discards the partial methods signature and calls the default methods in LINQ to SQLLINQ to SQL.

Nota

Se si utilizza Visual Studio, è possibile utilizzare il Object Relational DesignerObject Relational Designer per aggiungere la convalida e altre personalizzazioni alle classi di entità.If you are using Visual Studio, you can use the Object Relational DesignerObject Relational Designer to add validation and other customizations to entity classes.

Ad esempio, il mapping predefinito per la classe Customer nel database di esempio Northwind include il seguente metodo parziale:For example, the default mapping for the Customer class in the Northwind sample database includes the following partial method:

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

È possibile implementare un metodo personalizzato aggiungendo codice analogo al seguente alla classe parziale personalizzata Customer:You can implement your own method by adding code such as the following to your own partial Customer class:

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

Questo approccio viene utilizzato in genere LINQ to SQLLINQ to SQL per eseguire l'override di metodi predefiniti per Insert, Update, Deletee per convalidare le proprietà durante gli eventi del ciclo di vita di oggetto.This approach is typically used in LINQ to SQLLINQ to SQL to override default methods for Insert, Update, Delete, and to validate properties during object life-cycle events.

Per altre informazioni, vedere metodi parziali (Visual Basic) o parziale (metodo) (riferimenti per c#) (c#).For more information, see Partial Methods (Visual Basic) or partial (Method) (C# Reference) (C#).

EsempioExample

DescrizioneDescription

Nell'esempio riportato di seguito viene illustrato innanzitutto come definire ExampleClass usando uno strumento per la generazione di codice quale SQLMetal, quindi come è possibile implementare solo uno dei due metodi.The following example shows ExampleClass first as it might be defined by a code-generating tool such as SQLMetal, and then how you might implement only one of the two methods.

CodiceCode

// 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

EsempioExample

DescrizioneDescription

Nell'esempio riportato di seguito viene usata la relazione tra le entità Shipper e Order.The following example uses the relationship between Shipper and Order entities. Notare fra i metodi i metodi parziali InsertShipper e DeleteShipper.Note among the methods the partial methods, InsertShipper and DeleteShipper. Questi metodi eseguono l'override dei metodi parziali predefiniti forniti da LINQ to SQLLINQ to SQL mapping.These methods override the default partial methods supplied by LINQ to SQLLINQ to SQL mapping.

CodiceCode

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

Vedere ancheSee Also

Creazione e invio di modifiche dei datiMaking and Submitting Data Changes
Personalizzazione di operazioni di inserimento, aggiornamento ed eliminazioneCustomizing Insert, Update, and Delete Operations