适用于所有项目的应用程序范围设置

发布于 2024-09-07 07:02:15 字数 156 浏览 2 评论 0原文

我有一个包含 3 个项目的解决方案,每个项目都需要访问某些设置。我正在寻找一种方法,使这些设置值可用于来自 1 个不同来源的任何项目。我无法使用 .Config 文件,因为它与该特定项目相关。

我可以使用数据库,但被告知这不是一个好的做法(没有理由)

有什么想法吗?

I have a Solution with 3 projects in, each project needs access to certain settings. I'm looking for a way to have these setting values available to any project from 1 distinct source. I cant use the .Config file as that is relevent to that particular project.

I could use the database but have been told this is not good practice (Without an reason)

Any ideas?

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

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

发布评论

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

评论(6

半世晨晓 2024-09-14 07:02:15

您可以执行以下操作:

  • 在每个项目的 app.config 的解决方案文件夹

    中创建一个 solution.config

  • ,并将其添加到您的 < /code> 节点:

    
      ....
    
    

您必须将符号链接放置到您的公共 < code>solution.config 在每个项目文件夹中 - 但您可以拥有一个在项目之间共享的物理文件。

节点是唯一允许此类“累积”设置的节点 - file= 中指定的文件中的设置将添加到您的应用设置中,但可能会被您在 app.config 中明确指定的任何内容覆盖。

另一方面,是的,当然,您可以使用数据库。我们在大多数项目中也这样做,因为我们通常可以访问数据库,但不能访问客户端服务器计算机中的文件系统。我不明白为什么这一定是一件坏事 - 我们在表中设置了 DEV、TEST 和 PROD - 所以你将所有设置都放在一个地方 - 我们在需要时选择我们需要的设置。工作得很好 - 当然,一些设置(例如数据库的连接字符串)不能存储在那里 - 但我们的大部分配置信息是。再说一次:我真的不明白为什么这本身就是一个糟糕的选择 - 所以除非你的消息来源可以用一些事实和理由支持他/她的陈述,否则我会忽略它。

You could do this:

  • create a solution.config in your solution folder
  • in each project's app.config, add this to your <appSettings> node:

    <appSettings file="solution.config">
      ....
    </appSettings>
    

You would have to put symbolic links to your common solution.config in each project folder - but you could have one single physical file that you share amongst the projects.

The <appSettings> node is the only one that allows that sort of "cummulative" settings - those from the file specified in the file= will be added to your app settings, but potentially overwritten by anything you specify explicitly in your app.config.

On the other hand, yes, of course, you could use the database. We do that, too, in most of our projects, since we typically do have access to the database, but not to the file system in the client's server machines. I don't see why that should necessarily be a bad thing - we have settings for DEV, TEST and PROD in a table - so you have all your settings in one place - and we pick those settings we need when we need them. Works just fine - of course, some settings like the connection strings to the database cannot be stored there - but the bulk of our config info is. Again: I really don't see any reason why this should be a bad choice per se - so unless your source can back his/her statement up with some facts and reasons, I'd ignore it.

尽揽少女心 2024-09-14 07:02:15

您可以在已定义的 configSection 中定义 configSource 属性,以引用从中加载属性的外部文件。
在这里您可以找到一个示例:

App.config 文件是否可以引用另一个完整的配置文件? (.NET)

当然,您也可以使用数据库,但这可能涉及开发某种配置控制台,因为将配置属性直接管理到数据库中并不是一个好的做法。

否则,您可以创建您的配置文件(例如 xml 或 yaml)并创建您自己的共享配置解析器。

You can define configSource attribute in a defined configSection, to reference an external file from which to load your properties.
Here you can find an example:

Is there any way for an App.config file to reference another full config file? (.NET)

You can also use a DB of course, but that would probably involve developing some kind of configuration console, since it's not a good practice to manage config attributes directly into DB.

Otherwise you can create your config file (an xml, or yaml for example) and create your own shared config parser.

苦笑流年记忆 2024-09-14 07:02:15

我创建一个类来使用单例模式或全局实例(无论您偏好哪种)来保存系统范围的设置。

如果解决方案中存在另一个项目,它可以看到该类(当添加引用时)。

这还将设置的表示与存储机制(数据库、配置文件、自定义 XML 文件等)解耦,并且如果您设计接口,它也会使单元测试更加灵活。

I create a class to hold system-wide settings using either a Singleton pattern, or a Global instance (whichever you preference is).

If another project is in the solution, it can see the class (when references are added).

This also decouples the presentation of the settings from the storage mechanism (database, config file, custom XML file, whatever), and if you design to the interface, it makes unit testing more flexible, too.

两仪 2024-09-14 07:02:15

您可以在每个项目的 .config 文件中添加一个指向全局项目的条目。不过,您需要在三个地方阅读该内容。

我想到的另一个解决方案是使用自己的 .config 文件将常用设置放入自己的程序集中。然后,您将该程序集包含在您的每个项目中。在程序集中读取 .config 文件,您可以读出所需的值。

You could add an entry into each projects .config file that points to the global one. You would need to read that in three places though.

Another solution that springs to mind is to put your common settings into their own assembly with it's own .config file. You then include that assembly in each of your projects. The .config file is read in the assembly and you can read out those values you need.

仅一夜美梦 2024-09-14 07:02:15

有哪些类型的设置?

您可以使用系统范围的 machine.config 和 web.config 文件来设置应用于整个计算机的设置。

\Windows\Microsoft.NET\Framework[64]\[version]\config\machine.config
\Windows\Microsoft.NET\Framework[64]\[version]\config\web.config

What kinds of settings?

You can use the system wide machine.config and web.config files for settings that apply across an entire machine.

\Windows\Microsoft.NET\Framework[64]\[version]\config\machine.config
\Windows\Microsoft.NET\Framework[64]\[version]\config\web.config
黯淡〆 2024-09-14 07:02:15

如果您有权访问注册表,则可以使用它。那么您所需要的只是一个类来读出它们(并且可能有一个类将它们放入),并且每个项目都可以使用该类来读取它们。

但主要缺点是您必须将设置添加到运行解决方案的每台计算机注册表中。

You could use the registry if you have access to it. Then all you would need is a class to read them out (and possiblty one to put them in) and each project could use that class to read them.

The major downside though is that you would have to add the settings to each machines registry that you run your solution on.

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