Share via


Saklı Yordamları Kullanarak İşlemleri Özelleştirme

Saklı yordamlar, varsayılan davranışı geçersiz kılmaya yönelik yaygın bir yaklaşımı temsil eder. Bu konudaki örneklerde, saklı yordamlar için oluşturulan yöntem sarmalayıcılarını nasıl kullanabileceğiniz ve saklı yordamları doğrudan nasıl çağırabileceğiniz gösterilmektedir.

Visual Studio kullanıyorsanız ekleme, güncelleştirme ve silme işlemleri gerçekleştirmek üzere saklı yordamlar atamak için Nesne İlişkisel Tasarımcısı kullanabilirsiniz.

Not

Veritabanı tarafından oluşturulan değerleri yeniden okumak için saklı yordamlarınızda çıkış parametrelerini kullanın. Çıkış parametrelerini kullanamıyorsanız, Nesne İlişkisel Tasarımcısı tarafından oluşturulan geçersiz kılmalara güvenmek yerine kısmi bir yöntem uygulaması yazın. Veritabanı tarafından oluşturulan değerlere eşlenen üyeler, veya UPDATE işlemler başarıyla tamamlandıktan sonra INSERT uygun değerlere ayarlanmalıdır. Daha fazla bilgi için bkz . Varsayılan Davranışı Geçersiz Kılmada Geliştiricinin Sorumlulukları.

Örnek 1

Aşağıdaki örnekte, sınıfının türetilmiş bir sınıfta geçersiz kılmalar için kullanılan saklı yordamları çağırmak için iki yöntem içerdiğini varsayalım Northwind .

[Function()]
public IEnumerable<Order> CustomerOrders(
    [Parameter(Name = "CustomerID", DbType = "NChar(5)")]
    string customerID)
{
    IExecuteResult result = this.ExecuteMethodCall(this,
        ((MethodInfo)(MethodInfo.GetCurrentMethod())),
        customerID);
    return ((IEnumerable<Order>)(result.ReturnValue));
}

[Function()]
public IEnumerable<Customer> CustomerById(
    [Parameter(Name = "CustomerID", DbType = "NChar(5)")]
    string customerID)
{
    IExecuteResult result = this.ExecuteMethodCall(this,
        ((MethodInfo)(MethodInfo.GetCurrentMethod())),
        customerID);
    return (IEnumerable<Customer>)(result.ReturnValue);
}
<[Function]()> _
Public Function CustomerOrders( _
    <Parameter(Name:="CustomerID", DbType:="NChar(5)")> ByVal _
    customerID As String) As IEnumerable(Of Order)

    Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, _
        (CType(MethodInfo.GetCurrentMethod(), MethodInfo)), _
        customerID)
    Return CType(result.ReturnValue, IEnumerable(Of Order))
End Function

<[Function]()> _
Public Function CustomerById( _
    <Parameter(Name:="CustomerID", DbType:="NChar(5)")> ByVal _
        customerID As String) As IEnumerable(Of Customer)

    Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, _
        CType(MethodInfo.GetCurrentMethod(), MethodInfo), _
        customerID)

    Return CType(result.ReturnValue, IEnumerable(Of Customer))
End Function

Örnek 2

Aşağıdaki sınıf geçersiz kılma için bu yöntemleri kullanır.

public class NorthwindThroughSprocs : Northwind
{

    public NorthwindThroughSprocs(string connection) :
        base(connection)
    {
    }

    // Override loading of Customer.Orders by using method wrapper.
    private IEnumerable<Order> LoadOrders(Customer customer)
    {
        return this.CustomerOrders(customer.CustomerID);
    }
    // Override loading of Order.Customer by using method wrapper.
    private Customer LoadCustomer(Order order)
    {
        return this.CustomerById(order.CustomerID).Single();
    }
    // Override INSERT operation on Customer by calling the
    // stored procedure directly.
    private void InsertCustomer(Customer customer)
    {
        // Call the INSERT stored procedure directly.
        this.ExecuteCommand("exec sp_insert_customer …");
    }
    // The UPDATE override works similarly, that is, by
    // calling the stored procedure directly.
    private void UpdateCustomer(Customer original, Customer current)
    {
        // Call the UPDATE stored procedure by using current
        // and original values.
        this.ExecuteCommand("exec sp_update_customer …");
    }
    // The DELETE override works similarly.
    private void DeleteCustomer(Customer customer)
    {
        // Call the DELETE stored procedure directly.
        this.ExecuteCommand("exec sp_delete_customer …");
    }
}
Public Class NorthwindThroughSprocs : Inherits Northwind
    Sub New()
        MyBase.New("")
    End Sub
    ' Override loading of Customer.Orders by using method wrapper.
    Private Function LoadOrders(ByVal customer As Customer) As  _
        IEnumerable(Of Order)
        Return Me.CustomerOrders(customer.CustomerID)
    End Function

    ' Override loading of Order.Customer by using method wrapper.
    Private Function LoadCustomer(ByVal order As Order) As Customer
        Return Me.CustomerById(order.CustomerID).Single()
    End Function

    ' Override INSERT operation on Customer by calling the
    ' stored procedure directly.
    Private Sub InsertCustomer(ByVal customer As Customer)
        ' Call the INSERT stored procedure directly.
        Me.ExecuteCommand("exec sp_insert_customer …")
    End Sub

    ' The UPDATE override works similarly, that is, by
    ' calling the stored procedure directly.
    Private Sub UpdateCustomer(ByVal original As Customer, ByVal _
        current As Customer)
        ' Call the UPDATE stored procedure by using current
        ' and original values.
        Me.ExecuteCommand("exec sp_update_customer …")
    End Sub

    ' The DELETE override works similarly.
    Private Sub DeleteCustomer(ByVal customer As Customer)
        ' Call the DELETE stored procedure directly.
        Me.ExecuteCommand("exec sp_delete_customer …")
    End Sub
End Class

Örnek 3

komutunu tam olarak kullandığınız gibi kullanabilirsiniz NorthwindThroughSprocsNorthwnd.

NorthwindThroughSprocs db = new NorthwindThroughSprocs("");
var custQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

foreach (Customer custObj in custQuery)
    // deferred loading of cust.Orders uses the override LoadOrders.
    foreach (Order ord in custObj.Orders)
        // ...
        // Make some changes to customers/orders.
        // Overrides for Customer are called during the execution of the
        // following:
        db.SubmitChanges();
Dim db As New NorthwindThroughSprocs()
Dim custQuery = From cust In db.Customers _
                Where cust.City = "London" _
                Select cust

For Each custObj In custQuery
    ' Deferred loading of cust.Orders uses the override LoadOrders.
    For Each ord In custObj.Orders
        ' ...
        ' Make some changes to customers/orders.
        ' Overrides for Customer are called during the execution 
        ' of the following:
        db.SubmitChanges()
    Next
Next

Ayrıca bkz.