M
M
Melz2017-04-09 13:31:08
.NET
Melz, 2017-04-09 13:31:08

How to properly inject Properties.Settings into a project?

Hello.
There is a "small legacy project on WinForms". There's a little poltergeist there with settings that are Properties.Settings. In general, they began to understand and do refactoring with the removal of controls from the main project to the control libraries.
The program has settings that it uses (surprise!). Controls are also read and written (users can tick boxes through the form).
To deal with the poltergeist, they made an abstraction over Settings, an interface with all the necessary fields in the settings and Save methods, etc.

public interface ISettingProvider
    {
        int MyInt { get; set; }
        string MyString { get; set; }

        void Save();
    }

Accordingly, the project has a class for the interface that uses settings through getters / seters
public string MyString
        {
            get => Properties.Settings.Default.MyString;
            set => Properties.Settings.Default.MyString = value;
        }

IN TESTS, the interface was wet and we looked for a request to the settings.
mock.VerifyGet(...)
As you understand, controls can read settings here and there, including from projects with controls, although the settings are called Properties.Settings.Default, they are essentially different classes and controls are written not in the settings of the main project, but in the settings themselves.
Usually, you would just pass the class as a parameter to the constructor, but these are different classes. Those main project knows that there is CL with controls, but controls do not need to know about the main project at all. Otherwise, a cross-reference is obtained.
The classic way to overcome such garbage is to bring the necessary code into your own project. The library refers to it. The principal refers to it. The library does not know about the main thing.
Resharper tells you when the settings are no longer used by anyone :)
In general, the cons are also understandable (if they exist at all).
Am I thinking right?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question