There is a class for post, get requests:

public class ExternalCommunicationManager { private string _serverUrl; public ExternalCommunicationManager() { _serverUrl = ConfigurationManager.AppSettings["ServerUrl"]; } public async Task<IRestResponse> PostRequest(string url, string data, string token = null) { var client = new RestClient(_serverUrl + url); var request = CreateRestRequest(Method.POST, token); request.AddParameter("application/json; charset=utf-8", data, ParameterType.RequestBody); // execute the request var response = await client.ExecuteTaskAsync(request); return response; } ... private static RestRequest CreateRestRequest(Method method, string token) { var request = new RestRequest { Method = method, RequestFormat = DataFormat.Json, Timeout = 30000 }; request.AddHeader("Authorization", token); return request; } } 

There are also several classes that are injected via ninjects like InSingletonScope

Payment Manager, UserManager, and so on.

Actually the question is, how is it better and more correct to inherit the class that makes the requests and call the methods of the parent class, or inject it as a SingletonScope inside these managers into the constructor and call it through a private field there?

    1 answer 1

    The easiest option is not to inherit, and not to make it singleton. Just inject as an addiction.

    Rationale:

    • There is no need for inheritance. The ExternalCommunicationManager can send a line to the specified url. It is quite obvious that UserManager should not allow sending any line to any url through itself at all - its responsibility should be limited to specific requests relating specifically to users.
    • There is no need to do ExternalCommunicationManager Singleton-th - you do not have anything in the code that should obviously exist in exactly one instance. Those. you can make it a singleton - but it will in fact save you a maximum of one request in the ConfigurationManager.AppSettings per instance - i.e. almost nothing.
    • yes, when I wrote about singleton, I meant exactly as a dependency in a ninject, naturally it will be just an interface and its implementation in the form of a Binding <IMyManager> (). To <MyManager> (). InSingletonScope (); UserManager is also an example of the name, naturally there will be other managers, not user management, - Artem Polishchuk
    • In principle, the question is that I will have about 5 real managers who will use the ExternalCommunicationManager calls and how best to connect it to these managers, inject as dependencies, or inherit - Artem Polishchuk
    • @JULIK is the essence of the answer - do not inherit, inject as an addiction, but not necessarily as a singleton - PashaPash