全球VS辛格尔顿在.NET [英] Global vs Singleton in .NET

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

问题描述

我这里有一个很常见的情况。而多年来,我还没有发现,如果我在做什么是对的行业standards.Consider它连接到数据库的应用程序,但其中连接字符串,而不是存储在某些文件/设置被传递作为命令行参数在启动或数据库浏览到在应用程序启动的时间。

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.

那么有必要的地方保存连接字符串的应用程序的范围之内。我已经看到它做最常见的方式是一个模块或全局类get / set方法来保存连接字符串。另一种方式,我会做它用辛格尔顿。 无论是选择我的DAL可以访问连接字符串时,它需要通过一个GetConnectionString方法。

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.

理想的解决办法是让你的应用程序中加载了连接串出的配置和注入它变成任何类别需要它。根据您的应用程序的大小,一个的IoC容器喜欢的Unity 温莎城堡可以帮助,但肯定不是该解决方案的必要组成部分。

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.

如果这不是一种选择,你坚持维护(因为现有codeBase的或其他)全局状态,我不知道,有两种方法你建议之间存在着巨大的差异。

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.

更新:只是为了澄清,忘记所有关于的IoC容器的东西,现在,注入是说传递作为参数(无论是在类的构造函数只是一种奇特的方式,或通过财产,或其他)。

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.

更新#2:我觉得还是有一些误解,以什么这种方法需要

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();
    }
}

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

这些 <一href="http://googletesting.blogspot.com/2008/12/static-methods-are-death-to-testability.html">articles (事实上​​,许多在该博客的文章)细节的很多原因,后者比前者(至少是因为前者几乎是不可能的单元试验)。

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).

是的,在一些地方的有将要负责抓在连接字符串中的首位一些静态的家伙,但问题是,你的依赖静态方法仅限于一个地方(这很可能将是你在引导启动应用程序的过程中主要的方法),而不是整个整个codeBase的撒。

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辛格尔顿在.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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