单身样式的替代方案? [英] Alternatives for the singleton pattern?

查看:98
本文介绍了单身样式的替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经是网络开发人员一段时间了,现在使用ASP.NET和C#,我想通过使用最佳做法尝试增加我的技能。

I have been a web developer for some time now using ASP.NET and C#, I want to try and increase my skills by using best practices.

我有一个网站我想加载设置一次,只需要引用它,我需要它。所以我做了一些研究,50%的开发人员似乎都在使用单例模式来做到这一点。另外50%的开发商都是蚂蚁单身人士。他们都讨厌单身人士。他们建议依赖注入。

I have a website. I want to load the settings once off, and just reference it where ever I need it. So I did some research and 50% of the developers seem to be using the singleton pattern to do this. And the other 50% of the developers are ant-singleton. They all hate singletons. They recommend dependency injection.

为什么单身人士不好?加载网站设置的最佳做法是什么?它们应该只加载一次,并在需要时引用?我会怎么做这样做依赖注入(我是新的)?有人可以为我的场景推荐任何样品吗?我也想看到一些单元测试代码(对于我的场景)。

Why are singletons bad? What is best practice to load websites settings? Should they be loaded only once and referenced where needed? How would I go about doing this with dependency injection (I am new at this)? Are there any samples that someone could recommend for my scenario? And I also would like to see some unit test code for this (for my scenario).

感谢
Brendan

Thanks Brendan

推荐答案

一般来说,我避免单身,因为它们更难单位测试你的应用程序。单身人士很难模拟单元测试,正是因为它们的性质 - 你总是得到相同的,而不是一个可以轻松配置单元测试。配置数据 - 强类型的配置数据,无论如何 - 是一个例外,我做。通常,配置数据是相对静态的,替代方案涉及编写相当数量的代码,以避免框架提供的访问web.config的静态类。

Generally, I avoid singletons because they make it harder to unit test your application. Singletons are hard to mock up for unit tests precisely because of their nature -- you always get the same one, not one you can configure easily for a unit test. Configuration data -- strongly-typed configuration data, anyway -- is one exception I make, though. Typically configuration data is relatively static anyway and the alternative involves writing a fair amount of code to avoid the static classes the framework provides to access the web.config anyway.

使用它的几种不同的方法,仍然允许您单元测试您的应用程序。一种方法(可能是两种方式,如果您的单身人士不懒惰阅读app.cofnig),则在您的单元测试项目中设置默认的app.config文件,提供测试所需的默认值。您可以使用反射来替换单元测试中所需的任何特定值。通常,我将配置一个私有方法,允许在测试设置中删除私有单例实例,如果我对特定测试进行更改。

There are a couple of different ways to use it that will still allow you to unit test you application. One way (maybe both ways, if your singleton doesn't lazily read the app.cofnig) is to have a default app.config file in your unit test project providing the defaults required for your tests. You can use reflection to replace any specific values as needed in your unit tests. Typically, I'd configure a private method that allows the private singleton instance to be deleted in test set up if I do make changes for particular tests.

另一种方法是实际上并不直接使用单例,而是为单引擎类创建一个接口。您可以使用手动注入接口,如果提供的值为空,则默认为单例实例。这允许您创建一个可以传递给测试类的模拟实例,但在实际代码中,使用单例实例。基本上,需要它的每个类都保留对单例实例的私有引用并使用它。我喜欢这样一个更好的一点,但是由于单例将被创建,您可能仍然需要默认的app.config文件,除非所有的值都被懒加载。

Another way is to not actually use the singleton directly, but create an interface for it that the singleton class implements. You can use hand injection of the interface, defaulting to the singleton instance if the supplied value is null. This allows you to create a mock instance that you can pass to the class under test for your tests, but in your real code use the singleton instance. Essentially, every class that needs it maintains a private reference to the singleton instance and uses it. I like this way a little better, but since the singleton will be created you may still need the default app.config file, unless all of the values are lazily loaded.

public class Foo
{
    private IAppConfiguration Configuration { get; set; }

    public Foo() : this(null) { }

    public Foo( IAppConfiguration config )
    {
        this.Configuration = config ?? AppConfiguration.Instance;
    }

    public void Bar()
    {
         var value = this.Config.SomeMaximum;
         ...
    }
}    

这篇关于单身样式的替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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