C # console application script / store

What would be the best way to implement the psuedo-session / local storage class in a console application? Basically, when the application starts, I will take a few arguments that I want to make available to each class while the application is running. I thought I could just create a static class and initialize the values ​​when the application starts, but is there a more elegant way?

+1


a source to share


6 answers


I usually create a static class called "ConfigurationCache" (or something like that) that can be used to provide application configuration settings.

Keep in mind that you don't want to get too carried away with globals. I seriously recommend taking a look at your design and passing only what you need via method parameters. You must be such that each method receives a parameter for whatever is needed (see Code Complete 2 - Steve McConnell).



This does not mean that the static class is wrong, but ask yourself why you need to pass parameters to your various classes and methods.

+3


a source


If you want to take command line arguments (or some other super-duper setting) and put them somewhere that your application can see, I don't know why you think it is "inelegant" to put them in a static class when the application starts ... This is similar to what you want to do.



+1


a source


you can use the singleton design pattern if you want an object that you can pass in your code, but imo a static class is good too.

+1


a source


To be honest, I think the most elegant way would be to rethink your design to avoid "globals". Classes must be created or receive data to be built; methods must work with this data. You are breaking encapsulation by creating globals for the class or classes to do their job.

+1


a source


I would suggest that it is possible to implement a singleton class to manage your psuedo-session data. You will be able to access data globally, ensuring that only one instance of the class exists and remains consistent as long as you share your objects.

MSDN Implementation of Singleton Class

0


a source


Think of your data as a configuration file required by all of your classes. The file will be accessible from every class, so there is nothing wrong with exposing the data to a static class.

But every class needs to know the path to the configuration file, and changing the path will affect many classes. (Of course, a path should be better than a constant in only one class, which is referenced by all classes rewriting the path.) Thus, the best solution would be to create a class that encapsulates access to the configuartion file. Now each class can create an instance of this class and access the configuration data of the file. Since your data is not supported by the file, you will need to create something like a monostat.

Now you can start thinking about chaining classes. This is important for you? Are you planning to write a unit test and have to mock the config data? Yes? In this case, you should start thinking about using dependency injection and interace-only data access.

So I suggest using dependency injection with an interface, and I would implement the interface with the monostate pattern.

0


a source







All Articles