QuestionsCategory: C# Application from Start to Finishhow data passes from GlobalConfig Connection to IDataConnection
dinushka95 asked 9 months ago
  1. why is GlobalConfig class static?
  2. In GlobalConfig class  we choose the method of data saving way (InitializeConnections) (sql or text) and later get it and save it to a variable called  “Connection” (IDataConnection).

    And when calling we call globleconfig.connection.createPrize. so that “connection” can be “sql” or “text”.    

    i’m wondering how the IDataConnection class knows how to shift to “sqlConnector class” or “textconnector class”. It is saved in the “Connection” variable,

    but there is no place in the code where they check “Connection” is “sql” then run sqlConnector class method or if it’s “text” run textconnector class. Is it a feature of interfaces..?


RayT replied 4 months ago

I am a little bit confused as to how the CnnString method in the GlobalConfig class in the TrackerLibrary gets hold of the connection string in the AppConfig in the TrackerUI project?

The TrackerUI has a reference to the TrackerLibrary but not the other way?


Tim Corey replied 4 months ago

Good question. So the library does does not run by itself. Instead, it gets brought into a UI via reference so when it goes to look for a config file, it looks for the config file of the overall application that it is in. That is the config file of the UI. Look in the bin folder of your UI in either the debug or release sub-folder. You will see your UI (an exe file) and the library (a dll file) in there. That is how the dll gets access to the config – it is part of the same overall application.

1 Answers
Tim Corey answered 9 months ago

GlobalConfig is static because we want everyone to use the same data. If it were instantiated, we would need to pass the instance reference around. Since it is static, everyone can just use the data directly and everyone has access to the same data.

Yep, this is a feature of an interface. You can stick any class in it, as long as it implements the interface. Then, you can use the properties and methods specified by the interface when interacting with the class, regardless of what actual class type it is. That means that the rest of our code doesn’t need to care which database we are saving to or which specific implementation it is writing to. It just knows that there is a Connection variable and it holds a class that implements the interface IDataConnection. That is called loose coupling. The application doesn’t know about the database and the database layer doesn’t know about the application. If we needed to replace the database access layer with a different one (like we do with SQL vs Text Files), we just swap it out. Nothing else in the code needs to be changed.

dinushka95 replied 9 months ago

Thanks, now i understand.
why is it only running one interface ? shouldn’t it run both “sqlconnector” and “textconnector” class.
we do save the text “sql” or “text’ to a variable of IDataConnection in globe config class, but no where in the code we told like:-
if (connection==”sql”) {then use sqlConnector class}
if (connection==”text”){ then use textConnector class}
but some how it automatically knows which class to use by just saving “sql” or “text”

dinushka95 replied 9 months ago

i went thought the code again, we are saving not text. instance of “sqlconnector” and “textconnector” class
now i fully understand..!
thanks for all the help..

Your Answer