简体   繁体   English

使用此 static class 有哪些潜在问题

[英]What are the potential issues using this static class

Here is my sample code:这是我的示例代码:

public static class MySqlHelper
{
    private static string constring = ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;

    public static int ExecuteNonQuery(string mysqlquery)
    {
        SqlConnection conn = new SqlConnection(connString);
        SqlCommand cmd = new SqlCommand(mysqlquery, conn);
        int result;

        try
        {
            conn.Open();
            result= cmd.ExecuteNonQuery();
        }
        finally
        {
            conn.Close();
        }
        return result;
    }
}

Usage: MySqlHelper.ExecuteNonQuery("select * from customers");用法: MySqlHelper.ExecuteNonQuery("select * from customers");

I would like to know the issues using this static class.我想知道使用这个 static class 的问题。

I can change my class as mentioned here but I have been using this class in couple of websites and I will need couple days to change it in every place and test it out.我可以按照此处所述更改我的 class,但我一直在几个网站中使用此 class,我需要几天时间在每个地方进行更改并对其进行测试。

Thanks for any inputs.感谢您的任何意见。

Edit: Updated the code.编辑:更新了代码。 Does that make difference on the answers provided?这对提供的答案有影响吗? Sorry, I should have posted in the beginning.对不起,我应该在开始时发布。

I am assuming the connection string doesnt change during execution (you might want to make it readonly).我假设连接字符串在执行期间不会改变(您可能希望将其设为只读)。 Since there is no other shared state shown in the question, there are no real problems.由于问题中没有显示其他共享 state,因此没有实际问题。

However, if you have any shared state you have a huge threading problem.但是,如果您有任何共享的 state,那么您将遇到一个巨大的线程问题。 And if you have shared connections you have an even bigger problem.如果你有共享连接,你就会遇到更大的问题。

But as written, with no significant static fields: no problem.但正如所写,没有显着的 static 字段:没问题。

I thought he was pretty clear on his answer.我认为他的答案很清楚。

Writing my own Provider Class in ASP.NET 在 ASP.NET 中编写我自己的 Provider Class

"remember that the connection will be shared at the same instance for all users, which can cause bad problems..." “请记住,所有用户都将在同一实例上共享连接,这可能会导致严重问题......”

Static classes are difficult to test. Static 类很难测试。

Your question mentions that for a change you'll have to change the class on a couple websites.您的问题提到要进行更改,您必须在几个网站上更改 class 。 If your websites were coupled to an interface, then swapping out the implementation would be relatively simple.如果您的网站与接口耦合,那么替换实现将相对简单。

In my own projects I avoid public static classes.在我自己的项目中,我避免使用公共 static 类。 They can get unwieldy very quickly.他们很快就会变得笨拙。

As such no issue it depends how you are using this class, ideally it part of abstract factory pattern to have static connections so that same connection can be used all over application, but having methods such as executereader and other mthods is not a good choice.因此没有问题,这取决于您如何使用此 class,理想情况下,它是抽象工厂模式的一部分,具有 static 连接,以便可以在整个应用程序中使用相同的连接,但是使用诸如执行阅读器和其他方法之类的方法不是一个好的选择。 similarly always check that connection has not been closed or its state before its usage, because if you are having static connection and you used using syntax with executereader then it will close the connection and if some other method used connection after this then it will get the error同样,在使用之前始终检查连接是否尚未关闭或其 state,因为如果您有 static 连接并且您使用了与 executereader 一起使用的语法,那么它将关闭连接,如果在此之后使用其他方法连接,那么它将获得错误

The obvious drawbacks are:明显的缺点是:

  • hardcoded connection string硬编码的连接字符串
  • without connection pooling in the driver this is very bad: //open conn + //close conn如果驱动程序中没有连接池,这非常糟糕: //open conn + //close conn

1) can be ugly solved by using a singleton as the connection string name 1)可以通过使用 singleton 作为连接字符串名称来解决丑陋的问题

2) cannot be solved in a static class without introducing shared data: ie. 2)如果不引入共享数据,则无法在 static class 中解决:即。 without planting a knife in your back.不用在你的背上插刀。

Do you know that the MySQL Connector for .NET now has a MySqlHelper class (you can check it in the source here )?您知道用于 .NET 的 MySQL 连接器现在有一个 MySqlHelper class(您可以在此处查看源代码)?

If you are going to take this route you many want to consider using the Microsoft DAAB SqlHelper v2 offering.如果您打算采用这条路线,很多人都想考虑使用 Microsoft DAAB SqlHelper v2 产品。 It uses the same concept but is much more robust.它使用相同的概念,但更健壮。 It also caches SqlParameters.它还缓存 SqlParameters。 The code came out circa 2001 and has been in constant use by many developers.该代码大约在 2001 年问世,并一直被许多开发人员使用。

http://www.microsoft.com/download/en/details.aspx?id=435 http://www.microsoft.com/download/en/details.aspx?id=435

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

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