直接使用设置还是通过包装类?

发布于 2024-10-10 04:40:18 字数 1033 浏览 0 评论 0原文

参考 .NET 设置机制,并受到 this< 的几个答案的启发/a> 问题,似乎有些人主张通过另一个包装类(也许是单例)来包装 Settings 的使用。

Settings 类不是已经是单例了吗?

[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

    private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

    public static Settings Default {
        get {
            return defaultInstance;
        }
    }
// rest of class...
}

.
.
.
那么,将 Settings 包装在另一个类中并执行此操作有什么好处:

int foo = MySettingsWrapper.MyInt;

而不是在需要时直接执行此操作:

int foo = Settings.Default.MyInt;

With reference to the .NET Settings mechanism, and inspired by a couple of the answers to this question, it seems that some people advocate wrapping the use of Settings via another wrapper class, perhaps a singleton.

Isn't the Settings class a singleton already?

[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

    private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

    public static Settings Default {
        get {
            return defaultInstance;
        }
    }
// rest of class...
}

.
.
.
So, what is the benefit of wrapping Settings in another class and doing this:

int foo = MySettingsWrapper.MyInt;

instead of doing this directly where needed:

int foo = Settings.Default.MyInt;

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

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

发布评论

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

评论(3

人生戏 2024-10-17 04:40:19

Settings 类本身并不是单例。你可以这样做:

var s1 = new Settings();
var s2 = new Settings();
var s3 = new Settings();

但是,我相信这不会给你带来多大好处,因为据我所知,s1s2s3的值都会保留在同一位置:

c:\Documents and Settings>\\[Local Settings\]Application Data\\__\< ;verison>\user.config

所以我怀疑:

s1.MyInt = 1;
s1.Save();

将与以下相同

s2.MyInt = 1;
s2.save();

因此,您将获得 Settings.Default 静态属性,该属性返回单个线程安全实例。我一直按原样使用它,看不出有任何理由要包装它。但是,您可能想要扩展它,例如,如果您想控制 user.config 的位置:如何使设计器生成的.Net应用程序设置可移植

The Settings class is not a singleton per se. You could do:

var s1 = new Settings();
var s2 = new Settings();
var s3 = new Settings();

However, I believe it won't do you much good, since AFAIK the values of s1, s2 and s3 will all be persisted in the same place:

c:\Documents and Settings>\<username>\[Local Settings\]Application Data\<companyname>\<appdomainname>_<eid>_<hash>\<verison>\user.config

So I suspect:

s1.MyInt = 1;
s1.Save();

will be the same as

s2.MyInt = 1;
s2.save();

For that reason, you get the Settings.Default static property, which returns a single, thread safe instance. I use it all the time as it is, don't see any reason to wrap it. However, you might want to extend it, for example if you wanted to control the location of user.config: How to make designer generated .Net application settings portable

蓝戈者 2024-10-17 04:40:19

它是一种依赖注入。

Settings.Default 是设置机制的一种实现类型。

添加包装器意味着实现代码位于一处,而不是跨越您的代码。

将该文件替换为另一个实现将很容易,而无需更改代码。

可能有点矫枉过正。您需要决定是否需要更换该实现。但想一想,您可能会进一步发现一些其他系统可能需要访问这些设置(SQL 作业、远程服务等)。

通常,这个“包装器”应该是接口的实现,并且您可以使用控制反转容器(例如 Castle.Windsor)将实现注入到运行时环境中。

Its a type of dependency injection.

Settings.Default is 1 type of implementation of a settings mechanism.

Adding the wrapper means the implementation code is in one place, not across your code.

It would be easy to swap out that file for another implementation without needing to change your code.

Can be overkill. You need to decide whether you would ever need to swap out that implementation. But think about it, you might find further on down the line some other system that might need to access those settings (SQL Jobs, Remote Services etc).

Typically this "wrapper" should be an implementation of an interface, and you would use a inversion of control container (e.g. Castle.Windsor) to inject the implementation into the runtime environment.

东走西顾 2024-10-17 04:40:19

我认为使用基于接口的类包装 Settings 类的主要好处是有助于单元测试:避免文件 I/O、设置文件位置问题、启用依赖注入。

I think the main benefit to wrapping the Settings class with an interface-based class is helps with unit testing: avoids file I/O, settings file location issues, enables Dependency Injection.

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