Configuration File

To handle multiple configuration files.

The ConfigurationFile<T> class is a part of the Chase CommonLib library, developed by LFInteractive LLC. It serves as a template for creating configuration files in JSON format for .NET 6.0+ applications. This class is designed to be used as a base class for specific configuration file implementations, allowing developers to easily create and manage configuration files with common functionality. The class includes features for saving and loading configuration data from disk, as well as events that notify when the configuration file is saved or loaded.

Class Overview

namespace Chase.CommonLib.FileSystem.Configuration
{
    public class ConfigurationFile<T> where T : ConfigurationFile<T>, new()
    {
        // Class members and methods
    }
}

Class Constraints

The class is generic and takes a type parameter T, which must derive from ConfigurationFile<T> and have a parameterless constructor. This constraint ensures that any derived class adheres to the common functionality defined by this base class.

Properties

Path Property

[JsonIgnore]
public string Path { get; set; } = "";
  • This property represents the path to the configuration file on disk.

  • The [JsonIgnore] attribute is used to indicate that this property should be excluded when serializing the object to JSON. This allows developers to store additional information in the configuration file class without it being persisted to the file.

Events

ConfigurationSaved Event

  • This event is triggered when the configuration file is saved using the Save() method.

ConfigurationLoaded Event

  • This event is triggered when the configuration file is loaded using the Load() method.

Methods

Save() Method

  • This method saves the current configuration object to the specified file path.

  • It checks if the Path property is set and raises an exception if it is not.

  • The configuration object is serialized to JSON format with indented formatting and saved to the specified file path.

  • After saving, the ConfigurationSaved event is invoked to notify listeners.

Load() Method

  • This method loads a configuration object from the specified file path.

  • It checks if the Path property is set and raises an exception if it is not.

  • If the file does not exist, it creates a new configuration file by calling the Save() method.

  • If the file exists, it reads the JSON content, deserializes it into the specified type T, and sets the Path property of the loaded object.

  • After loading, the ConfigurationLoaded event is invoked to notify listeners.

  • The method returns the loaded configuration object or null if loading fails.

Example Usage

Here's an example of how to use the ConfigurationFile<T> class and how to apply the [JsonIgnore] and [JsonProperty] attributes:

In this example, we create a derived configuration class MyAppConfiguration with additional properties. We use [JsonIgnore] to exclude the SecretKey property from serialization and [JsonProperty] to rename the LogLevel property in the output JSON.

Last updated

Was this helpful?