Use dependency injection for Properties.Settings.Default?

If you consider using Properties.Settings.Defaultinside a class as a dependency and therefore introduce it?

eg:.

public class Foo
{
    private _settings;
    private bool _myBool;

    public Foo(Settings settings)
    {
        this._settings = settings;
        this._myBool = this._settings.MyBool;
    }
}

.
,

or do you think use Settingsas a global global practice is good practice?

eg:.

public class Foo
{
    private bool _myBool;

    public Foo()
    {
        this._myBool = Properties.Settings.Default.MyBool;
    }
}
+5
source share
3 answers

, . , , , . IoC, .

ConfigurationManager WebConfigurationManager, . , , .

+2

" " :

public class Foo
{
    private readonly bool _myBool;

    public Foo(bool myBool)
    {
        _myBool = myBool;
    }
}

Foo , . Foo , .

+4

They should be transmitted as dependency. Imagine a scenario when you want to change this set of parameters using Unit Test in your example # 2 - you will need to have some kind of complex switching logic in the static get agent to do this.

Many IoC containers can even provide a singleton implementation when introducing classes that help you.

+2
source

All Articles