ObjectDataSourceDisposingEventArgs クラス
アセンブリ: System.Web (system.web.dll 内)


ObjectDataSourceDisposingEventArgs クラスは、OnObjectDisposing メソッドで使用されます。このクラスにより、ObjectDataSource コントロールとビジネス オブジェクトを使用している任意のデータ操作を実行した後、そのビジネス オブジェクトが破棄されるまでの間、ビジネス オブジェクトのインスタンスにアクセスできます。ビジネス オブジェクトへのアクセスは、ObjectInstance プロパティを使用して行います。ObjectDisposing イベントを処理するデリゲートを追加することで、ビジネス オブジェクトの、パブリックとして公開された任意のメンバにアクセスし、任意の最終的な作業またはクリーンアップを実行できます。
データ操作を実行するメソッドが static メソッドの場合、OnObjectDisposing メソッドは ObjectDataSource コントロールによって呼び出されません。メソッドが静的な場合、ビジネス オブジェクトのインスタンスは作成されません。
ObjectDataSource コントロールは、有効期間中、基になるビジネス オブジェクトを操作する際に利用できるイベントを数多く公開しています。イベント、および関連する EventArgs クラスとイベント ハンドラのデリゲートの一覧を次の表に示します。
EventArgs | ||
---|---|---|
ObjectCreating. | ObjectDataSourceEventArgs | ObjectDataSourceObjectEventHandler |
ObjectCreated. | ObjectDataSourceEventArgs | ObjectDataSourceObjectEventHandler |
Selecting. | ObjectDataSourceSelectingEventArgs | ObjectDataSourceSelectingEventHandler |
Inserting、Updating、および Deleting。 | ObjectDataSourceMethodEventArgs | ObjectDataSourceMethodEventHandler |
ObjectDataSourceStatusEventArgs | ObjectDataSourceStatusEventHandler | |
ObjectDataSourceStatusEventArgs | ObjectDataSourceStatusEventHandler | |
ObjectDisposing. | ObjectDataSourceDisposingEventArgs | ObjectDataSourceDisposingEventHandler |

このセクションには、2 つのコード例が含まれています。ビジネス オブジェクトおよび GridView コントロールで、ObjectDataSource コントロールを使用して情報を表示する方法を最初のコード例に示します。最初のコード例で使用した中間層ビジネス オブジェクトの例を提供するコードの例を 2 番目に示します。
ビジネス オブジェクトおよび GridView コントロールで、ObjectDataSource コントロールを使用して情報を表示する方法を次のコード例に示します。Web ページで実行される各データ操作に、作成時の負荷が大きい (作成に時間がかかる、リソースの消費が激しいなど) ビジネス オブジェクトを使用している可能性があります。負荷のかかるオブジェクトを使用する場合は、データを操作するたびにインスタンス化と破棄を繰り返すのではなく、インスタンス化したオブジェクトを後続の処理で再利用できるようにキャッシュに格納しておくことをお勧めします。この例では、このパターンを示します。ObjectCreating イベントを処理することで、キャッシュにオブジェクトが存在するかどうかをチェックしてから、存在しない場合にのみ、インスタンスを作成できます。次に、ObjectDisposing イベントを処理し、ビジネス オブジェクトを破棄せずに、キャッシュに保存して後で使用できるようにします。この例では、ObjectDataSource がインスタンスで Dispose メソッドを呼び出さないように、ObjectDataSourceDisposingEventArgs オブジェクトの CancelEventArgs.Cancel プロパティが true に設定されています。
<%@ Import namespace="Samples.AspNet.VB" %> <%@ Page language="vb" %> <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> <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>
<%@ Import namespace="Samples.AspNet.CS" %> <%@ Page language="c#" %> <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> <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>
前のコード例で使用した中間層ビジネス オブジェクトの例を提供するコードの例を次に示します。このコード例は、状態を維持しビジネス ロジックをカプセル化する EmployeeLogic クラスによって定義される、基本ビジネス オブジェクトで構成されます。実際に動作させるためには、このコードをライブラリとしてコンパイルして、ASP ページからこれらのクラスを使用する必要があります。
Imports System Imports System.Collections Imports System.Web.UI Imports System.Web.UI.WebControls Namespace Samples.AspNet.VB Public Class EmployeeLogic Public Sub New() MyClass.New(DateTime.Now) End Sub 'New Public Sub New(ByVal creationTime As DateTime) _creationTime = creationTime End Sub 'New Private _creationTime As DateTime ' Returns a collection of NorthwindEmployee objects. Public Function GetCreateTime() As ICollection Dim al As New ArrayList() ' Returns creation time for this example. al.Add("The business object that you are using was created at " + _creationTime) Return al End Function 'GetCreateTime End Class 'EmployeeLogic End Namespace ' Samples.AspNet.VB
namespace Samples.AspNet.CS { using System; using System.Collections; using System.Web.UI; using System.Web.UI.WebControls; // // EmployeeLogic is a stateless business object that encapsulates // the operations you can perform on a NorthwindEmployee object. // public class EmployeeLogic { public EmployeeLogic () : this(DateTime.Now) { } public EmployeeLogic (DateTime creationTime) { _creationTime = creationTime; } private DateTime _creationTime; // Returns a collection of NorthwindEmployee objects. public ICollection GetCreateTime () { ArrayList al = new ArrayList(); // Returns creation time for this example. al.Add("The business object that you are using was created at " + _creationTime); return al; } } }


System.EventArgs
System.ComponentModel.CancelEventArgs
System.Web.UI.WebControls.ObjectDataSourceDisposingEventArgs


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- ObjectDataSourceDisposingEventArgs クラスのページへのリンク