.Net All Application Variables

I am new to .Net ... I have several requests that will run when the application loads. I want these queries to store the data in an object (dataset?) That is available throughout the application. Should I be using a singleton class? How does a custom control in my application refer to public variables in the main application code?

I guess I haven't found a good resource on how / where to store application variables and how to reference them when I need to populate a ListBox, DataGridView, etc.

As a background, I'm most familiar with developing using Flex Builder 3, sorry for the vague question ... I'm having a problem finding good help on this topic (not just populating the control, but storing data at the application level and referencing it from anywhere in the app).

Edit: This is for programming a Windows Forms Application using C #

+2


source to share


4 answers


It looks like you are using ASP.NET, in which case Application State (MSDN) will allow you to store and retrieve applications - which can be accessed from anywhere in the application.

More details here:

How to store values ​​in application state

How to read values ​​from application state



If you are writing a desktop application, you should create a static class containing your application data, for example:

public static class ApplicationSettings
{
    public static string InstallDirectory { get { ... } set { ... } };
    public static DataSet SomeDataSet { get { ... } set { ... } };


    static ApplicationSettings()
    {
       // ... initialize or load settings here
    }
}

      

There is no need to use a singleton here, but if you need lazy initialization and threading support, you can take this route.

+2


source


You can store the information in the App.config file and use the AppSettingsReader class to access the data.



EDIT: Seeing that you don't want to query for information multiple times, you can use a Singleton to access and cache the data.

+1


source


Presumably, your objects will be needed while the main application form is open. If so, just save them as form properties.

0


source


Singletons are bad, m'kay?;)

Or, moreover, global data (especially volatile global data) is generally not very good. This makes the classes difficult to test and debug. Small coverage is good volume.

One option is to look at the IoC container library (as well as the DI framework).

IoC = Inversion of Control DI = Dependency Injection (or Inverse)

Basically, you can set up constructors on your classes that need access to global data, and add your "singleton" type parameter - except it's not a singleton, just a plain old object (or interface). Then you tell the container that your "global dataset" is long lasting and uses the container to create other objects. You will no longer use the keyword "new". The advantage is that the container will automatically wire everything up for you, creating one and only one instance of the global class and injecting it into all other constructed objects.

Here's an (incomplete) list of libraries / frameworks for .NET:

IoC Container Benchmark

Do not specify another. I'm using Unity, but that doesn't mean it's best for you. Here's another list: http://elegantcode.com/2009/01/07/ioc-libraries-compared/

0


source







All Articles