惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
The Last Watchdog
The Last Watchdog
T
Tenable Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
V
Vulnerabilities – Threatpost
F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
A
Arctic Wolf
云风的 BLOG
云风的 BLOG
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
GbyAI
GbyAI
阮一峰的网络日志
阮一峰的网络日志
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
MyScale Blog
MyScale Blog
B
Blog
Spread Privacy
Spread Privacy
S
Schneier on Security
Project Zero
Project Zero
L
LINUX DO - 热门话题
M
MIT News - Artificial intelligence
F
Full Disclosure
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
Cyberwarzone
Cyberwarzone
AWS News Blog
AWS News Blog
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tailwind CSS Blog
K
Kaspersky official blog
Recent Announcements
Recent Announcements
NISL@THU
NISL@THU
Cisco Talos Blog
Cisco Talos Blog
S
Securelist
P
Privacy & Cybersecurity Law Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Exploit Database - CXSecurity.com
V
Visual Studio Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Webroot Blog
Webroot Blog

博客园 - Avlee

ArcGIS Server Manager 登录失败的解决方法 利用ArcSDE C API读取ArcSDE Raster数据 倡议以赞助的形式成立一个博客园的开发团队(博客园商业化尚早) 在WebGIS中试用Microsoft Silverlight有感 The MSVC project of the cairo library(version 1.4.6) WebGIS团队启用二级域名 Building the cairo graphic library using msvc Adobe 将停止对Adobe SVG Viewer的支持! SVG Authoring Guidelines[转] ArrayList.ToArray(Type) Or ArrayList.CopyTo(Array) 关于旧mdl程序向V8升级的问题 有了Flash和SVG,Adobe还想做什么呢? 基于Geomedia Professional平台的GIS应用开发(一) 根据权限创建页面上的功能按钮的一种简单有效的方法 如何让英文版的Adobe SVG Viewer显示中文文字 Adobe SVG Viewer 6.0 中自定义右键菜单 过年了,再忙也要回家...... 申请加入 “WebGIS” 团队 WebGIS团队刚刚成立
Using Delegates with Data Readers to Control DAL Responsibility[转]
Avlee · 2005-12-28 · via 博客园 - Avlee

原文:http://aspalliance.com/articleViewer.aspx?aId=526&pId=-1

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 still exists. 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 the connection and reader open for a longer time or reacquire the result set, increasing the time the connection is in use.

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 again invoked by the class/method which receives the delegate as an argument.

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 then closes the data reader and the database connection.

Code

DAL

public class DataComponent
{
 //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 * from authors where au_id=@ID",conn);
   command.Parameters.Add("@ID",SqlDbType.NVarChar,11).Value=authorID; 
   conn.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 id
  if(!IsPostBack)
   DataComponent.GetAuthor("172-32-1176",new DataComponent.IDataReaderHandler(BindGrid)); 
 }


 //Bind the datagrid when this is called by the delegate
 private void BindGrid(IDataReader reader)
 {
  DataGrid1.DataSource = reader;
  DataGrid1.DataBind();
 }
}
Conclusion

The point is that data readers can be dangerous if they aren’t in control. With this simple idea you can mitigate the potential threat of running out of resources and maybe even consider passing data readers to the client in proper scenarios. It’s still not as flexible as the data table but with this you can build data readers as part of your DAL to get the maximum flexibility out of ADO.NET.