ObjectDataSourceDisposingEventArgs.ObjectInstance プロパティ

定義

ObjectDataSource コントロールがデータ操作を行うときに使用するビジネス オブジェクトを表すオブジェクトを取得します。

public:
 property System::Object ^ ObjectInstance { System::Object ^ get(); };
public object ObjectInstance { get; }
member this.ObjectInstance : obj
Public ReadOnly Property ObjectInstance As Object

プロパティ値

ObjectDataSource がデータ操作を実行するために使用するビジネス オブジェクト。それ以外で nullObjectDataSourceEventArgs に渡される場合は、null

次のコード例では、ビジネス オブジェクトとコントロールでコントロールを ObjectDataSource 使用して情報を GridView 表示する方法を示します。 (時間またはリソースの観点から) 非常にコストの高いビジネス オブジェクトを使用して、Web ページが実行するすべてのデータ操作を作成する場合があります。 コストの高いオブジェクトを操作する方法の 1 つは、そのインスタンスを 1 回作成してから、すべてのデータ操作に対して作成および破棄するのではなく、後続の操作用にキャッシュすることです。 この例では、このパターンを示します。 イベントをObjectCreating処理して、最初にオブジェクトのキャッシュをチェックしてから、インスタンスを作成できます (まだキャッシュされていない場合のみ)。 次に、イベントを ObjectDisposing 処理して、ビジネス オブジェクトを破棄するのではなく、将来使用するためにキャッシュします。 この例では、 CancelEventArgs.Cancel クラスの ObjectDataSourceDisposingEventArgs プロパティを に設定してtrue、 インスタンスで を呼び出Disposeさないよう指示ObjectDataSourceします。

<%@ Import namespace="Samples.AspNet.CS" %>
<%@ Page language="c#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

// Instead of creating and destroying the business object each time, the 
// business object is cached in the ASP.NET Cache.
private void GetEmployeeLogic(object sender, ObjectDataSourceEventArgs e)
{
    // First check to see if an instance of this object already exists in the Cache.
    EmployeeLogic cachedLogic;
    
    cachedLogic = Cache["ExpensiveEmployeeLogicObject"] as EmployeeLogic;
    
    if (null == cachedLogic) {
            cachedLogic = new EmployeeLogic();            
    }
        
    e.ObjectInstance = cachedLogic;     
}

private void ReturnEmployeeLogic(object sender, ObjectDataSourceDisposingEventArgs e)
{    
    // Get the instance of the business object that the ObjectDataSource is working with.
    EmployeeLogic cachedLogic = e.ObjectInstance as EmployeeLogic;        
    
    // Test to determine whether the object already exists in the cache.
    EmployeeLogic temp = Cache["ExpensiveEmployeeLogicObject"] as EmployeeLogic;
    
    if (null == temp) {
        // If it does not yet exist in the Cache, add it.
        Cache.Insert("ExpensiveEmployeeLogicObject", cachedLogic);
    }
    
    // Cancel the event, so that the object will 
    // not be Disposed if it implements IDisposable.
    e.Cancel = true;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - C# Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"          
          datasourceid="ObjectDataSource1">
        </asp:gridview>

        <asp:objectdatasource 
          id="ObjectDataSource1"
          runat="server"          
          selectmethod="GetCreateTime"          
          typename="Samples.AspNet.CS.EmployeeLogic"
          onobjectcreating="GetEmployeeLogic"
          onobjectdisposing="ReturnEmployeeLogic" >
        </asp:objectdatasource>        

    </form>
  </body>
</html>
<%@ Import namespace="Samples.AspNet.VB" %>
<%@ Page language="vb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

' Instead of creating and destroying the business object each time, the 
' business object is cached in the ASP.NET Cache.
Sub GetEmployeeLogic(sender As Object, e As ObjectDataSourceEventArgs)

    ' First check to see if an instance of this object already exists in the Cache.
    Dim cachedLogic As EmployeeLogic 
    
    cachedLogic = CType( Cache("ExpensiveEmployeeLogicObject"), EmployeeLogic)
    
    If (cachedLogic Is Nothing) Then
            cachedLogic = New EmployeeLogic            
    End If
        
    e.ObjectInstance = cachedLogic
    
End Sub ' GetEmployeeLogic

Sub ReturnEmployeeLogic(sender As Object, e As ObjectDataSourceDisposingEventArgs)
    
    ' Get the instance of the business object that the ObjectDataSource is working with.
    Dim cachedLogic  As EmployeeLogic  
    cachedLogic = CType( e.ObjectInstance, EmployeeLogic)
    
    ' Test to determine whether the object already exists in the cache.
    Dim temp As EmployeeLogic 
    temp = CType( Cache("ExpensiveEmployeeLogicObject"), EmployeeLogic)
    
    If (temp Is Nothing) Then
        ' If it does not yet exist in the Cache, add it.
        Cache.Insert("ExpensiveEmployeeLogicObject", cachedLogic)
    End If
    
    ' Cancel the event, so that the object will 
    ' not be Disposed if it implements IDisposable.
    e.Cancel = True
End Sub ' ReturnEmployeeLogic
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - VB Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"          
          datasourceid="ObjectDataSource1">
        </asp:gridview>

        <asp:objectdatasource 
          id="ObjectDataSource1"
          runat="server"          
          selectmethod="GetCreateTime"          
          typename="Samples.AspNet.VB.EmployeeLogic"
          onobjectcreating="GetEmployeeLogic"
          onobjectdisposing="ReturnEmployeeLogic" >
        </asp:objectdatasource>        

    </form>
  </body>
</html>

注釈

データ操作メソッド (SelectMethodUpdateMethodDeleteMethodおよびInsertMethod) がインスタンス メソッドの場合、メソッドが実行される前にビジネス オブジェクトのインスタンスが作成されます。 イベント ハンドラーの プロパティObjectDisposingに オブジェクトを保存することで、呼び出しごとにビジネス オブジェクトがObjectInstance作成されないようにすることができます。 後続の ObjectCreating イベントでは、 プロパティからビジネス オブジェクトを ObjectInstance 取得できます。

適用対象

こちらもご覧ください