ConfigurationSettings 与 Properties.Settings

发布于 2024-09-30 14:39:07 字数 428 浏览 1 评论 0原文

我有一个 Winform 应用程序,当前有 16 个 SQL 连接存储在 DAL 项目 Settings.settings 中。

我正在尝试编写一个“Manager”类来简化这个( 此处)。但是,我在网上找到的大多数示例似乎都使用 ConfigurationSettings.AppSettings["something"]。同时,我正在使用Properties.Settings.Default.something。

有人可以解释一下哪个被认为是正确的以及为什么对于桌面应用程序来说是正确的吗?

I have a Winform app that has 16 SQL Connections currently stored in the DAL Projects Settings.settings.

I am trying to write a "Manager" class to simplify this(like here). However, most of the examples I find on the web seem to use ConfigurationSettings.AppSettings["something"]. While, I WAS using Properties.Settings.Default.something.

Can someone, please, explain which is considered CORRECT and why for a Desktop applications?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

£烟消云散 2024-10-07 14:39:07

我认为正确的方法是使用app.config文件并将它们存储在connectionStrings部分?

然后你可以像这样访问它们:

 ConfigurationManager.ConnectionStrings["something"].ConnectionString

如果这太“丑陋”,你可以轻松地编写一个包装器。

public class ConnectionManager
{
    public string Something
    {
        get
        {
             // TODO: check to make sure the configuration exists and throw an exception perhaps
             return ConfigurationManager.ConnectionStrings["something"].ConnectionString;
        }
    }
}

糟糕...正如所指出的,我的意思是执行 ConfigurationManager 而不是 ConfigurationSettings。

I think the correct way is to use the app.config file and store them in the connectionStrings section?

Then you can access them like:

 ConfigurationManager.ConnectionStrings["something"].ConnectionString

You could write a wrapper around that easily if that is too "ugly".

public class ConnectionManager
{
    public string Something
    {
        get
        {
             // TODO: check to make sure the configuration exists and throw an exception perhaps
             return ConfigurationManager.ConnectionStrings["something"].ConnectionString;
        }
    }
}

Oops...as pointed out I meant to do ConfigurationManager and not ConfigurationSettings.

╰沐子 2024-10-07 14:39:07

我从来都不热衷于将 sql 连接字符串放入软件的配置文件中。用户习惯于出于好奇或愚蠢(或两者的某种组合)而搞砸它们。我喜欢将所有连接字符串(开发、模型、生产等)放入数据访问库的属性中,并在其中包含一个 ConfigurationSettings 类,我使用该类根据消费方设置的某些属性来访问它们应用程序:

public class ConfigurationSettings
{

    public static string MyConnectionString
    {
    get
            if(ConfigurationSettings.TestMode)
                return Properties.Settings.Default.MyConnectionStringTest;
            else
                return Properties.Settings.Default.MyConnectionStringProd;
    }
    }

    // I typically only have test and not-test. This could
    // easily be some other combination of values to satisfy
    // multiple environments.
    public static bool TestMode { get; private set;}
}

这允许我通过通用名称调用此类的静态属性,并根据某些条件使所有连接字符串可用。这可以让您的特定数据集、实体以及您使用的任何数据模型免于担心连接字符串,并且设置可以编译到 .dll 中(并且不再需要担心它们)。还可以修改此方法,以使用类似的方法从 app.config 中提取设置(我为 ASP.Net 站点执行此操作)。

更新:正如您所暗示的那样,确实没有“正确”的方法。 app.config 旨在保存无需重建应用程序即可修改的配置设置。属性设置旨在保存不可修改的设置。所以两者都是完全有效的。也许最“正确”的方法是对您的应用程序和开发团队都有意义的方法。

I've never been a big fan of putting sql connection strings into configuration files for software. Users have a habit of goofing them up out of curiosity or stupidity (or some combination of the two). I like to put all of my connection strings (development, model, production, whatever) into the Properties of my data access library, and include within it a ConfigurationSettings class that I use to access them based on some property that is set by the consuming application:

public class ConfigurationSettings
{

    public static string MyConnectionString
    {
    get
            if(ConfigurationSettings.TestMode)
                return Properties.Settings.Default.MyConnectionStringTest;
            else
                return Properties.Settings.Default.MyConnectionStringProd;
    }
    }

    // I typically only have test and not-test. This could
    // easily be some other combination of values to satisfy
    // multiple environments.
    public static bool TestMode { get; private set;}
}

This allows me to call the static properties of this class through a common name and have all connection strings available depending on some criteria. This gets your specific datasets, entities, whatever data model you're using from being in the business of worrying about connection strings and the settings can be compiled into the .dll (and no longer need to worry about them). This method can also be modified to pull settings from an app.config (which I do for ASP.Net sites) in a similar method.

UPDATE: There really is no "correct" way as you imply. The app.config was designed to hold configuration settings that are modifiable without rebuilding the application. Property Settings were designed to hold settings that are not modifiable. So both are perfectly valid. Probably the most "correct" way is a way that makes sense both for your application and for your development team.

菩提树下叶撕阳。 2024-10-07 14:39:07

我们更喜欢使用 Properties.Settings(又名 settings.settings),因为它是强类型的。不要尝试在同一配置文件中执行具有不同环境(开发/产品)的奇特技巧。每个环境都有一个单独的配置文件,并在部署配置时重命名配置要容易得多。即

app.config 
app.stage.config
app.test.config
app.prod.config

我们使用 PowerShell 脚本(批处理文件类固醇)来进行我们的部署。我将简化我们作为传统批处理文件所做的事情 - (伪代码/未经测试的示例):

@echo off
:: %1 is the environment name (first parameter)
setlocal
set source=c:\tfs\project\bin\release\
set target=\\server\share\path\

xcopy /s/e %source% %target%

:: Using a copy command to rename/overwrite the env specific version
if exists %target%\app.%1.config copy /y %target%\app.%1.config %target%\app.config

We prefer to use Properties.Settings (aka. settings.settings) because it's strongly typed. Don't try to do fancy tricks that have different environments (dev/prod) in the same config file. It is much easier to have a separate config file per environment, and to rename the configs when you deploy them. i.e.

app.config 
app.stage.config
app.test.config
app.prod.config

We use PowerShell scripts (batch files on steroids) to do our deployments. I'll simplify what we do as a traditional batch file - (pseudo code/untested sample):

@echo off
:: %1 is the environment name (first parameter)
setlocal
set source=c:\tfs\project\bin\release\
set target=\\server\share\path\

xcopy /s/e %source% %target%

:: Using a copy command to rename/overwrite the env specific version
if exists %target%\app.%1.config copy /y %target%\app.%1.config %target%\app.config
撑一把青伞 2024-10-07 14:39:07

Properties.Settings.Default 通常用于保存应用程序内部状态(例如背景颜色),以记住用户设置。

ConfigurationSettings 实际上是您正在讨论的“Manager”类,可以从 app.config 文件访问其他自定义设置集,包括连接字符串。

Properties.Settings.Default are generally used to save application internal state like the color of the background, to remember user settings.

ConfigurationSettings is actually the "Manager" class you were talking about and can access other custom sets of settings from the app.config file, including connection strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文