我如何改进ADO.NET的代码(最佳方法)? [英] How I Can Improve Code (Best approach) Of ADO.NET ?

查看:67
本文介绍了我如何改进ADO.NET的代码(最佳方法)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



下面有一些代码片段,请告诉我哪种方法更好。





方法一:



Hi Friends,

There is some code snippet below, please suggest me which approach is better.


Approach One:

STNList = new List<STNforGCN>();
               while (reader.Read())
               {
                   STNList.Add(LoadSTN(reader));
               }
               this.Connection.Close();



 private STNforGCN LoadSTN(IDataReader reader)
        {
            STNforGCN stn = new STNforGCN();
            stn.STNID = Convert.ToInt32(reader["STNID"]);
            stn.STNNumber = reader["STNNumber"].ToString();
            stn.GeneratedForEntityID = Convert.ToInt32(reader["GeneratedForEntityID"]);
            stn.Value = Math.Round(Convert.ToDecimal(reader["VALUE"]), 2);
            stn.BrandName = reader["BrandName"].ToString();
            stn.StnStatus = reader["Status"].ToString();
            return stn;
        }







方法二:




Approach Two:

 STNforGCN stn=null;
while (reader.Read())
               {
              
            stn = new STNforGCN();
            stn.STNID = Convert.ToInt32(reader["STNID"]);
            stn.STNNumber = reader["STNNumber"].ToString();
            stn.GeneratedForEntityID = Convert.ToInt32(reader["GeneratedForEntityID"]);
            stn.Value = Math.Round(Convert.ToDecimal(reader["VALUE"]), 2);
            stn.BrandName = reader["BrandName"].ToString();
            stn.StnStatus = reader["Status"].ToString();
             STNList.Add(stn);
               }
               this.Connection.Close();

推荐答案

方法一可能被人们认为是更好的。这样做的原因是它的设计更好,创建了一个 STNforGCN 对象,它与迭代结果列表分开。



至于性能,它们在所有实际场景中大致相同。您将不得不在方法一的结果中支付每行方法调用的开销,但与其他工作相比,这可能是微不足道的。



你在两种方法中分配和初始化相同数量的 STNforGCN 对象,方法二重用一个局部变量保存最多移动堆栈指针无论如何,这将是非常快的。



如果你真的关心性能你应该使用分析器而不是猜测,现代编译器可以做你真棒的事情因此,仅通过查看代码就很难确定绝对性能差异。例如,如果编译器决定在方法一中内联 LoadSTN ,那么解决方案几乎完全相同。



希望这会有所帮助,

Fredrik
Approach One is likely to be considered "better" by people. The reason for this is that its design is better with the creation of a STNforGCN object separated from iterating over the result list.

As for performance, they're about the same for all practical scenarios. You will have to pay the overhead of a method call per line in the result with approach one, but that is likely to be insignificant compared to the rest of the work carried out.

You are in both approaches allocating and initializing the same number of STNforGCN objects, the fact that Approach two re-uses a local variable saves at most moving the stack-pointer which is going to be very fast anyway.

If you're really concerned about performance you should use a profiler instead of guessing, modern compilers can do awesome things to your code so it is very difficult to determine absolute performance differences by just looking at the code. For example, if the compiler decides to inline LoadSTN in Approach One, then the solutions are pretty much identical.

Hope this helps,
Fredrik


这篇关于我如何改进ADO.NET的代码(最佳方法)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆