• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

如何改进我们的CM.AppSettings参考(How can I improve our CM.AppSettings references ...

原作者: [db:作者] 来自: 网络 收藏 邀请

如何改进我们的CM.AppSettings参考(How can I improve our CM.AppSettings references)

ASP.NET 3.5

我们的解决方案中的类引用了ConfigurationManater.AppSettings [“”]以获取appSettings(来自web.config)。

我们决定对此并不满意。 人们在代码中错误地键入appSetting键名(编译得很好),跟踪用法很麻烦。 然后在整个代码库中存在重复的字符串,因为您在整个地方引用了相同的appSettings。

因此,我们决定只允许一个类引用ConfigurationManager,并且当需要某个appSetting的值时,其余的解决方案将引用该类。 ConfigurationManater.AppSettings [“”]是静态的,因此我们从单个Settings类中暴露了一堆静态只读属性。

public class Settings {
    public static string Foo {
        get {
            return ConfigurationManager.AppSettings["Foo"];
        }
    }
}

这非常有效,直到我们需要在测试中模拟设置。 我们创建了一个界面来启用我们的模拟(这是一个错误吗?)。

public interface ISettings {
    string Foo {
        get;
        set;
    }

}

public class Settings : ISettings {
    public string Foo {
        get {
            return ConfigurationManager.AppSettings["Foo"];
        }
    }
}

现在我们将ISettings实例注入为使用设置值的对象的依赖项(类/接口在每个人都可以引用而没有问题的项目中)。

在我们无法注入现有实例的地方(例如Global.asax),我们在静态字段中构造一个新实例。

鉴于所有这些,你会建议我们改变什么,为什么?


ASP.NET 3.5

Classes throughout our solution referenced ConfigurationManater.AppSettings[""] to get appSettings (from web.config).

We decided we weren't happy with that. Folks were mistyping appSetting key names in code (which compiled fine), and it was cumbersome to track usages. And then there's the duplicated strings throughout the codebase as you reference the same appSettings all over the place.

So, we decided that only one class would be allowed to reference the ConfigurationManager, and the rest of the solution would reference that class when it needed the value of a certain appSetting. ConfigurationManater.AppSettings[""] was static, so we exposed a bunch of static read-only properties off of our single Settings class.

public class Settings {
    public static string Foo {
        get {
            return ConfigurationManager.AppSettings["Foo"];
        }
    }
}

That worked pretty well, until we needed to mock the settings in our tests. We created an interface to enable our mocking (was this a mistake of any kind?).

public interface ISettings {
    string Foo {
        get;
        set;
    }

}

public class Settings : ISettings {
    public string Foo {
        get {
            return ConfigurationManager.AppSettings["Foo"];
        }
    }
}

And now we're injecting the ISettings instance as a dependency of the objects which use settings values (the class/interface are in a project that everyone can reference without problems).

In places where we can't inject an existing instance (e.g. Global.asax), we construct a new instance into a static field.

Given all of that, what would you recommend we change, and why?


原文:https://stackoverflow.com/questions/2373335
更新时间:2022-03-22 08:03

最满意答案

使用界面来表示配置是个好主意。 但是你的实现看起来有些偏差。

Joshua Flanagan写道,编写应用程序配置代码的方式可以将特定的配置部分注入到代码中。 这是一个好主意,因为它真正解耦您的代码而不用担心配置背后的细节。 读一读

我认为这将解决您遇到的问题。 可测试性。


Using an interface to represent configuration is a good idea. But your implementation looks a little off.

Joshua Flanagan wrote about writing application configuration code in a way that specific configuration sections can be injected into your code. This is a good idea, as it really decouples your code from worrying about details behind configuration. Have a read.

I think this will address the issue you are having re. testability.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap