简体   繁体   English

减少C#中的使用次数

[英]Reducing number of using in C#

My question might be silly or may not be a question at all, but here it goes.. 我的问题可能是愚蠢的,或者根本不是一个问题,但在这里......

I am performing quite some database operations in my ASP.net MVC project where I am creating two objects each and every time namely, SqlConnection and SqlCommand . 我在我的ASP.net MVC项目中执行了一些数据库操作,我每次都在创建两个对象,即SqlConnectionSqlCommand

I have shown an example below 我在下面展示了一个例子

using (SqlConnection connection = new SqlConnection(connectionString))
{
  using (SqlCommand command = new SqlCommand("sRegisterUser", connection))
  {

I am doing all these different operations inside one class under different methods. 在不同方法的一个类中进行所有这些不同的操作

My question is how can I reduce the creation of these objects each and every time? 我的问题是如何每次都减少这些对象的创建? How can I create them globally 我如何在全球范围内创建它们

PS: sRegisterUser is a stored procedure, like wise other methods use different procedures which gets different values as parameters. PS:sRegisterUser是一个存储过程,就像明智的其他方法一样,使用不同的过程,它们将不同的值作为参数。

Please help me out. 请帮帮我。

Thanks for any help in advance. 在此先感谢您的帮助。

The answer is, don't. 答案是, 不要。 You do not want to share these objects, you are using them appropriately. 希望共享这些对象,你正确使用它们。

You don't. 你没有。 You either keep the object alive, which is bad. 你要么让对象保持活力,那就太糟糕了。 Or you dispose it the way you should, and currently do. 或者你按照自己应有的方式处理它,目前也是这样。

So, why do you have use using ? 那么,你为什么要using

Since using is necessary for disposing the handles in Windows. 由于在Windows中处理句柄需要using

You could also write this, which is similar to using using : 你也可以写这个,类似于using

SqlConnection connection = new SqlConnection(connectionString);
connection.Close();
connection.Dispose();

using also disposes when the code above throws an error. 当上面的代码抛出错误时, using也会处理。 Using using is actually the shortest way to code this. 使用using实际上是编写代码的最短方式。

So the error-save version should be: 所以错误保存版本应该是:

SqlConnection connection;

try
{
    connection = new SqlConnection(connectionString);

    ...

    connection.Close();
}
finally
{
    connection.Dispose();
}

As others already said, there is nothing wrong with what you're doing right now. 正如其他人已经说过的那样,你现在正在做的事情没有错。 ADO.NET objects are meant to be used and disposed of. ADO.NET对象旨在被使用和处理。 As a rule of thumb you should create them as late as possible and dispose of them as soon as possible . 根据经验,您应该尽可能晚地创建它们并尽快 处理它们

With that being said, I understand what you're trying to do here and I do have a few ideas you might be interested in for making your life easier while keeping good programming practices. 说到这里,我理解你在这里要做的事情,我确实有一些你可能感兴趣的想法,让你的生活更轻松,同时保持良好的编程习惯。

If you want to make the code more concise you can chain using statements: 如果您想使代码更简洁,可以using语句链接:

using (var cn = new SqlConnection(yourConnectionString))
using (var cmd = new SqlCommand(yourQuery, cn))
{
    // do stuff
}

The IDE will not try to indent the second block, making it easier on you to read. IDE不会尝试缩进第二个块,使您更容易阅读。

You can also create an abstraction layer for your procedure: 您还可以为您的过程创建一个抽象层

public static class Procedures
{
    public static void RegisterUser() // add whatever parameters you need
    {
        using (var cn = new SqlConnection(yourConnectionString))
        using (var cmd = new SqlCommand(yourQuery, cn))
        {
            // do stuff
        }
    }
}

From then on you can execute your procedure simply by calling: 从那时起,您只需调用以下命令即可执行您的程序:

Procedures.RegisterUser();

Another alternative is to introduce a factory pattern to get your objects. 另一种方法是引入工厂模式来获取对象。 This alone won't reduce their number but it might help in getting them all set up already (ie correct connection string). 仅这一点不会减少它们的数量,但它可能有助于将它们全部设置好(即正确的连接字符串)。

You also can be creative and combine the two patterns. 您也可以创造性地将两种模式结合起来。 A custom class implementing IDisposable can take care of creating the necessary ADO objects, open the connection, execute your query, close the connection and dispose of any objects that needs disposal when it is itself disposed of. 实现IDisposable的自定义类可以负责创建必要的ADO对象,打开连接,执行查询,关闭连接并处置在处理时需要处理的任何对象。

Take your pick. 随便挑选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM