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

推荐订阅源

I
Intezer
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
N
News | PayPal Newsroom
T
Tenable Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
P
Privacy International News Feed
IT之家
IT之家
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
博客园_首页
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
I
InfoQ
D
DataBreaches.Net
有赞技术团队
有赞技术团队
K
Kaspersky official blog
Security Latest
Security Latest
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic

博客园 - 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.