DLL中的App.config文件,最佳实践。 [英] App.config file in a DLL, best practice.

查看:344
本文介绍了DLL中的App.config文件,最佳实践。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dll,其中有多个对象,每个对象都需要读取一个配置文件。我正在使用xml直接读取文件,因为正如您所知,应用程序使用主程序集app.config。



我的问题是这样做的最佳做法是什么。我应该创建一个静态变量来存储我的xml阅读器,这样它只创建一次(因此只打开文件一次)(见下文)或者我应该在需要时打开和关闭文件。



想法?



我的尝试:



公共静态类MySettings

{

静态XMLSettings _sett = null;



static XMLSettings值

{

get

{

if(_sett = null)

_sett = new XMLSettings();

return _sett;

}

}

}

I have a dll that has multiple objects in it with each object needing to read a config file. I'm reading the file directly using xml because, as you know, the application uses the main assemblies app.config.

My question is what is the best practice for doing this. Should I create a static variable that stores my xml reader in it, so that it is only created once (and therefore only opens the file once) (see below) or should I open and close the file whenever needed.

Thoughts?

What I have tried:

public static class MySettings
{
static XMLSettings _sett=null;

static XMLSettings Values
{
get
{
if (_sett = null)
_sett = new XMLSettings();
return _sett;
}
}
}

推荐答案

没有最佳做法。只要做你要求的任何事情。这取决于你是否要支持动态更改的app.config。
There is no best practice. Just do whatever your requirements dictate. It depends on if you want to support a dynamically changed app.config or not.


首先,没有任何程序集或汇编模块真正访问App.config文件。此名称仅作为项目工件出现,如果存在,则通过其中一个构建步骤复制到输出目录,以在部署应用程序时用作默认应用程序配置文件。此文件的名称派生自程序集的条目程序集的主可执行模块的名称;比如说,如果你的应用程序的主(入口点)可执行模块是myTool.exe,配置将在myTool.exe.config中。



注意整点是用户可以手动或在某些自定义安装步骤中修改此文件的可能性。实际上,当不需要这种可能性时,这个配置文件将毫无意义。在这种情况下,应用程序的开发人员可能在某些资源中具有固定数据集,甚至是硬编码的。而且,项目不需要App.config来使用配置文件。 .NET运行时不知道有关项目中文件的任何信息,但如果找到格式正确且命名的配置文件,它将应用配置数据。始终应用数据的默认属性。关键是:当它依赖于这个运行时系统行为时,整个机制是有意义的。如果您还需要其他东西,最好按照自己的方式使用自己的自定义文件。 (请注意,还有设置文件,但我不想使用它们,因为持久性机制不够灵活,同时,它不提供任何我可以更轻松实现的功能。与配置不一样文件,在很多情况下确实应该使用。)



也就是说,我认为在其他程序集(DLL)中配置文件没有任何意义。您可以使用任何自定义持久性机制,除非您不想将所有设置委派给应用程序的主程序集。我强烈建议基于数据合同的XML或JSON文件。这种机制是最容易使用,最先进,最强大和非侵入性的。您只需定义数据模型,然后序列化程序将其存储并稍后恢复到完全相同的状态。请参阅:使用数据合同 [ ^ ]。



-SA
First of all, no assembly or assembly module really access "App.config" file literally. This name appears only as a project artifact, which is, if present, copied by one of the build steps to the output directory, to be served as a default application configuration file when the application is deployed. The name of this file is derived from the name of the main executable module of the entry assembly of the assembly; say, if you application's main (entry-point) executable module is "myTool.exe", the configuration will be in "myTool.exe.config".

Note that the whole point is the possibility to modify this file by the user, manually or during some custom installation step. Practically, when such possibility is not needed, this configuration file would be totally pointless. In this case, the developer of the application could have fixed data set, in some resources or even hard-coded. Moreover, the project does not need "App.config" to use the configuration file. The .NET runtime "doesn't know" anything about files in the project, but it applies the configuration data if properly formatted and named configuration file is found. Default properties of the data will always be applied. The point is: the whole mechanism makes sense when it relies on this runtime system behavior. If you need something else, you should better use your own custom file, the way you want. (Note that there are also setting files, but I would prefer not using them, because the persistence mechanism is not flexible enough, and, at the same time, it does not provide any features I could easier implement myself. Not the same with config files, which really should be used in many cases.)

That said, I see no sense in having configuration files in other assemblies ("DLLs"). You can use any custom persistence mechanism, unless you don't want to delegate all settings to the main assembly of the application. I would strongly recommend XML or JSON file based on data contract. This mechanism is the easiest to use, most advanced, robust and non-intrusive. You just define your data model, and the serializer stores it and later restores to exact same state. Please see: Using Data Contracts[^].

—SA


这篇关于DLL中的App.config文件,最佳实践。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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