views:

207

answers:

1

Hello SO-Followers,

I want to initialize a user configuration through a user configuration file. The path for the file can be read from the registry. The file is located in a folder with the user's name.

So I need the following functionality:

  • Reading a string from registry
  • building the path to the configuration file
  • Reading the file into a configuration object

Now there are several approaches to handle this:

First, I need

  • one "helper"-class for getting the file path (let's call it Shared)
  • one "container"-class for the configuration information (let's call it Configuration)

So, Shared has a function/property like UserConfigurationFile which returns the path to the configuration file.

To get the path to the file I have a function InitializeUserConfigurationFile() which is called in the constructor of Shared:

class Shared {
    public Shared()
    {
        InitializeUserConfigurationFile();
    }

    void InitializeUserConfigurationFile()
    {
        //
        // Reads username
        //

        //
        // Reads path from Registry
        //

        //
        // etc.
        //

    }


    //
    // etc.
    //
}

Any better suggestions?

When I want to Initialize my Container I have different options:

  • Is it best to initialize the user configuration within the constructor?

Sth. like:

class Container
{
    Shared shared = new Shared();

    public Container()
    {
        InitializeUserConfiguration();
    }

    void InitializeUserConfiguration()
    {
        LoadConfiguration(shared.UserConfigurationFile);
    }

    void LoadConfiguration(string filename)
    {
        //
        // Initializes all parameters frome filename
        //
    }
}
  • Or through two steps (through an own method LoadConfiguration())?

Sth. like:

Shared    shared    = new Shared();
Container container = new Container();

container.LoadConfiguration(shared.UserConfigurationFile);
  • Or inside the constructor of Container by delivering a filename?

Sth. like:

Shared    shared    = new Shared();
Container container = new Container(shared.UserConfigurationFile);

or everything in Container..?

There are so many ways...

I hope somebody knows a best-approch...

Regards,

Inno

+2  A: 

It is better to use standard configuration classes exist in .net. Such as ApplicationSettingsBase and Configuration.

Here you can find good article series:

  1. Unraveling the Mysteries of .NET 2.0 Configuration
  2. Unraveling the Mysteries of .NET 2.0 Configuration
  3. Cracking the Mysteries of .NET 2.0 Configuration
arbiter
And what to do if you have a given application which uses this kind of configuration (using own files) since years?
Inno
If it working, do not touch it. But you are asking for best approach, I suggest you best approach :)In general, using standard classes, you need to write less, spend less time, and make less errors. So maybe you need to think about configuration refactoring in one of the future version.
arbiter
"But you are asking for best approach, I suggest you best approach :) " yes, you did ;) But if your application doesn't support best approach but "some approach" you need the best approach to handle this "some approach" %-)
Inno