LFInteractive Docs
  • Programming Documentation
  • C#
    • Installing Visual Studio
    • Understanding C#
      • Namespaces
      • Classes
      • Enum
      • Variables
        • Static Types
        • Primitive Types
        • Access Modifiers
        • Variables vs Properties
        • Nullable Variables
      • Getters and Setters
      • Solution vs Project
      • Struct vs Class
      • Coding Conventions
      • Tasks and Async
        • Parallel Tasks
      • Methods
      • PreProcessor Statements
    • Creating Your First Console App
      • Class Library
        • Models
          • File Model
          • Result Model
        • Controllers
          • File Controller
          • File System Controller
      • Console App
        • Nuget Packages
        • Main Method
    • Minecraft.NET
      • Minecraft.NET Library
      • Modrinth.NET Library
      • CurseForge.NET Library
      • Fabric.NET Library
    • Common Lib
      • Strings
      • Advanced Network Client
      • Advanced Timer
      • Advanced File Info
      • Configuration File
      • Application Config
      • Database File
      • Crypt Class
  • C++
    • Networking
      • Windows Socket (Client)
    • cclip
    • VCPKG
    • spdlog
      • Getting Started
      • Patterns
Powered by GitBook
On this page
  • Properties
  • Methods
  • Example Code
  • Example Class

Was this helpful?

Edit on GitHub
  1. C#
  2. Common Lib

Application Config

A singleton for your application configuration.

The AppConfigBase class is a base class for configuration files in the Chase CommonLib library, designed for use in .NET 6.0 and later. It provides a foundation for managing configuration settings by extending the ConfigurationFile<T> class, where T is the derived configuration class. This base class implements a singleton pattern to ensure that only one instance of the configuration is created and managed during the application's lifetime.

Properties

Instance

[JsonIgnore]
public static T Instance { get; }
  • Description: Gets the singleton instance of the configuration file.

  • Usage: This property allows you to access the configuration settings throughout your application using the singleton pattern. It ensures that there is only one instance of the configuration, promoting consistency in configuration values.

Path

public string? Path { get; protected set; }
  • Description: Gets or sets the path to the configuration file on the disk.

  • Usage: This property is used to specify the location of the configuration file. It is set during the initialization process.

Methods

Initialize

public virtual void Initialize(string path)
  • Description: Initializes and loads the configuration file from the specified path.

  • Parameters:

    • path (string): The path to the configuration file.

  • Usage: Call this method to initialize the configuration file. It sets the Path property and loads the configuration file from the provided path. This method should be called once at the beginning of your application.

Load

public override T? Load()
  • Description: Loads the configuration file from disk.

  • Returns: The loaded instance of the derived configuration class.

  • Exceptions:

    • IOException: Thrown if the configuration file path is not set.

  • Usage: This method loads the configuration settings from the specified file into the derived configuration class. It also handles copying properties from the loaded instance to the singleton instance to ensure that the singleton instance reflects the latest configuration values.

Example Code

Here is an example of how to use the AppConfigBase class:

using Chase.CommonLib.FileSystem.Configuration;

// Define your derived configuration class
public class MyAppConfig : AppConfigBase<MyAppConfig>
{
    // Define your configuration properties here
    public string ApiKey { get; set; }
    public int MaxConnections { get; set; }
}

// In your application code:
public class Program
{
    public static void Main()
    {
        // Initialize the configuration
        MyAppConfig.Instance.Initialize("config.json");

        // Access configuration values
        string apiKey = MyAppConfig.Instance.ApiKey;
        int maxConnections = MyAppConfig.Instance.MaxConnections;

        // Modify configuration values (if needed)
        MyAppConfig.Instance.MaxConnections = 10;

        // Save the updated configuration (if needed)
        MyAppConfig.Instance.Save();
    }
}

In the above example, we first define a derived configuration class MyAppConfig based on AppConfigBase. We define configuration properties within this class.

In the Main method, we initialize the configuration using Initialize, access configuration values, and optionally modify and save them. The Instance property provides access to the singleton instance of the configuration.

By using this class, you can easily manage your application's configuration settings while ensuring consistency and singleton behavior.

Ignoring Properties

To ignore specific properties during serialization, you can use the [JsonIgnore] attribute. For example, if you want to ignore a property named IgnoreMe, you can do the following:

public class MyAppConfig : AppConfigBase<MyAppConfig>
{
    [JsonIgnore]
    public string IgnoreMe { get; set; }
}

Changing Property Names in JSON Output

To change the property name in the JSON output, you can use the [JsonProperty] attribute. For example, if you want to change the property name ApiKey to api_key in the JSON output, you can do the following:

public class MyAppConfig : AppConfigBase<MyAppConfig>
{
    [JsonProperty("api_key")]
    public string ApiKey { get; set; }
}

This allows you to control how your configuration properties are represented in the serialized JSON configuration file.

Example Class

public class TestConfig : AppConfigBase<TestConfig>
{
    [JsonProperty("test_string")]
    public string TestString { get; set; } = "Hello World!";
    
    [JsonProperty("test_int")]
    public int TestInt { get; set; } = 123;
    
    [JsonProperty("test_bool")]
    public bool TestBool { get; set; } = true;
    
    [JsonProperty("test_double")]
    public double TestDouble { get; set; } = 123.456;
    
    [JsonProperty("test_float")]
    public float TestFloat { get; set; } = 123.456f;
    
    [JsonProperty("test_decimal")]
    public decimal TestDecimal { get; set; } = 123.456m;
    
    [JsonProperty("test_guid")]
    public Guid TestGuid { get; set; } = Guid.NewGuid();
    
    [JsonIgnore]
    public DateTime TestDateTime { get; set; } = DateTime.Now;
    
    [JsonProperty("test_time_span")]
    public TimeSpan TestTimeSpan { get; set; } = TimeSpan.FromMinutes(5);
}
PreviousConfiguration FileNextDatabase File

Last updated 1 year ago

Was this helpful?