Using Delegates with Data Readers to Control Dal Responsibility

xiaoxiao2021-03-06  111

Why people don't use data readers

A problem with data readers is their connected nature. Every data reader has one database connection tied to it. With .NET 1.X the limitation is one open data reader per database connection, while .NET 2.0 brings new features which allow more than one Active Result Set Per Connection, But The Connection STISTS. This Means That Passing A Data Reader To The Client Means Also Passing The Responsibility of Closing The Data Reader and The Connection.

Developers also tend to avoid using data readers because they are read-only, forward-only and therefore a bit unsuitable for complex calculation scenarios which might involve the need to access data more than once. This means that with data readers you would need to keep ................

USING DELEGATES To Control Dal Responsibility

Delegates are a way to reference methods based on their signature and return type, allowing an instantiated delegate to reference a given method assuming that the method matches the delegate definition. Delegate instances can be passed as method parameters which means that methods themselves can be passed and .

Delegates are one solution to the previously mentioned responsibility problem with data readers. In most common data binding scenarios an IDataReader instance is pulled from the DAL, assuming data readers and the data binding are in use, resource releasing is left up to the client. With delegates we can turn this so that a data binding method taking an IDataReader instance as a parameter, is itself given as a parameter to the DAL. With this idea the responsibility of cleanup is up to the DAL, because DAL invokes the delegate instance, waits For it to finish execution and the closs the data ready and the database connection.code dal public class datcomponent

{

// delegate to Declare Accepted DataBinding Callback Method

Public Delegate Void iDataReaderHandler (iDataReader Reader);

// Get The Data Reader Callback Using Given Delegate

Public Static Void getAuthor (String Authorid, iDataReaderhandler Handler)

{

// Connection Scope - Using Pubs Database

Using (SqlConnection Conn = New SqlConnection ("Server = .; trusted_connection = true; database = pubs)))))

{

// query

Sqlcommand command = new sqlcommand ("SELECT * AUTHORS WHERE AU_ID = @ ID", CONN);

Command.Parameters.Add ("@ ID", sqldbtype.nvarchar, 11) .value = authorID;

Cn.open ();

// DataReader Scope - Calling The Delegate Method and Finishing IT Before

// going out of scope and closing the reader

Using (SqlDataReader Rdr = Command.executeReader (Commandbehavior.CloseConnection)

{

Handler (RDR);

}

}

}

} Page (code-behind) public class Webform1: System.Web.ui.page

{

Protected system.web.ui.webcontrols.dataGrid DataGrid1;

Private Void Page_Load (Object Sender, System.EventArgs E)

{

// Bind Grid with Given Author IDif (! Ispostback)

Datacomponent.getauthor ("172-32-1176", new datacomponent.idataraderhandler (bindgrid);

}

// bind the datagrid when this is caled by the delegate

Private void Bindgrid (iDataReader Reader)

{

DataGrid1.datasource = reader;

DataGrid1.databind ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-97081.html

New Post(0)