全球vs Singleton在.NET [英] Global vs Singleton in .NET

查看:103
本文介绍了全球vs Singleton在.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里很常见的情况。多年来,我没有发现我在做什么是行业标准的权利。考虑连接到数据库的应用程序,但是连接字符串而不是存储在某些文件/设置中的地址作为命令行参数传递在启动时,或者在应用程序启动时浏览数据库。



有必要将该连接字符串保存在应用程序范围内。我已经看到的最常见的方式是使用get / set方法来保存连接字符串的模块或全局类。另一种方式是使用Singleton。
当我需要通过GetConnectionString方法时,我的DAL可以访问连接字符串。



有没有更好的方法?



更新:我没有配置文件,即使我在应用程序实例的生命周期中需要连接字符串一次。您可以详细说明将其注入任何类部分

解决方案

在一般的全球状态下,无论是全球类还是单独的,应该尽可能避免。



理想的解决方案是让你的应用程序加载连接字符串的配置和将其注入任何需要它的类。根据应用程序的大小, IoC 容器,如 Unity Castle Windsor 可以帮助,但肯定不是解决方案的必要部分。



如果这不是一个选项,并且你坚持维持全球状态(因为的现有代码库或其他),我不知道您建议的两种方法之间有很大的区别。



更新:为了澄清,现在忘了所有关于IoC容器的东西,注入只是一个很好的方法,说传入作为参数(对于类的构造函数,或者通过一个属性,或者是什么)。 p>

与数据访问类不同,请求连接字符串(来自某种全局或单例),而不是通过构造函数或属性传入。



更新#2:我认为这种方法还有一些误会。 p>

它基本上归结于你的数据访问类是否如下所示:

  public class DataAccessClass 
{
public DataAccessClass()
{
_connString = SomeStaticThing.GetConnectionString();
}
}

  public class DataAccessClass 
{
public DataAccessClass(s​​tring connString)
{
_connString = connString;
}
}

这些 文章(事实上,该博客中的许多文章)详细说明了后者比前者更好的一些原因(特别是因为前者几乎不可能进行单元测试)。



是的,在某些地方将要有一些静态首先负责抓住连接字符串的人,但重要的是,您对静态方法的依赖性仅限于该位置(在引导应用程序的过程中可能会成为您的Main方法),而不是洒掉在整个代码库中。


I have a very common situation here. And for years I haven't found if what i am doing is RIGHT by industry standards.Consider an application which connects to the database, but where the connection string instead of being stored in some file/setting is being passed as a command line parameter at startup OR the database is browsed to at the time the application starts up.

Well it becomes necessary to save that connection string somewhere within the scope of the app. Most common way I have seen it done is a module or global class with get/set methods to save the connections string. Another way I would do it is using Singleton. Either options my DAL can access the connection string when it needs to through a GetConnectionString method.

Is there a BETTER way of doing this?

Update: i do not have a config file and even if I did I would need the connection string to be read once for the life of the application instance. can you elaborate on the "inject it into any classes" part

解决方案

In general global state, be it a global class or a singleton, should be avoided wherever possible.

The ideal solution would be to have your application load up the connection string out of config and inject it into any classes that need it. Depending on the size of your application, an IoC container like Unity or Castle Windsor can help, but certainly isn't a required part of the solution.

If that isn't an option and you're stuck maintaining global state (because of the existing codebase or whatever), I don't know that there's a huge difference between the two approaches you've suggested.

Update: Just to clarify, forget all the stuff about IoC containers for now, "Inject" is just a fancy way of saying "pass in as a parameter" (either to the class's constructor, or via a property, or whatever).

Rather than having a data access class have to ask for the connection string (from some sort of global or a singleton), have it passed in via the constructor or a property.

Update #2: I think there's still some misunderstanding as to what this approach entails.

It basically comes down to whether your data access class looks like:

public class DataAccessClass
{
    public DataAccessClass()
    {
        _connString = SomeStaticThing.GetConnectionString();
    }
}

or

public class DataAccessClass
{
    public DataAccessClass(string connString)
    {
        _connString = connString;
    }
}

These articles (and in fact, many of the articles in that blog) detail a number of reasons why the latter is better than the former (not least because the former is almost impossible to unit test).

Yes, at some place there is going to have to be some static guy responsible for grabbing the connection string in the first place, but the point is that your dependencies on static methods are limited to that one spot (which is likely going to be your Main method in the process of bootstrapping your application), rather than sprinkled throughout your whole codebase.

这篇关于全球vs Singleton在.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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