Skip to content
Yannik Höflich edited this page Sep 3, 2023 · 3 revisions

ConfigService

The config service is what you use to get informations from the config.

The service should only be used in a service that implements either IInitializable or IAsyncInitializable. Everything that you use in the config should be noted in the LayoutConfig.

public ExampleService : IService, IInitializable{
    public ConfigLayout ConfigLayout { get; } = new ConfigLayout(){
        ConfigName = "text-config",
        Keys = new ConfigKey[]{
            new("test-field", typeof(int))
        }
    };

    public InitResult Init() => InitResult.Success();
}

Now you inject the ConfigService.

public ExampleService : IService, IInitializable{
    private ConfigService _configService;

    public ConfigLayout ConfigLayout { get; } = new ConfigLayout(){
        ConfigName = "text-config",
        Keys = new ConfigKey[]{
            new("test-field", typeof(int))
        }
    };

    public ExampleService(ConfigService configService){
        _configService = configService;
    }

    public InitResult Init() => InitResult.Success();
}

Then you get the values from the ConfigService.

public ExampleService : IService, IInitializable{
    private ConfigService _configService;
    private int _testField;

    public ConfigLayout ConfigLayout { get; } = new ConfigLayout(){
        ConfigName = "text-config",
        Keys = new ConfigKey[]{
            new("test-field", typeof(int))
        }
    };

    public ExampleService(ConfigService configService){
        _configService = configService;
    }

    public InitResult Init(){
        var config = _configService.GetConfig("text-config");

        if(config is null){
            return InitResult.NoConfig();
        }

        if(config.TryGetInt("test-field", out int value)){
            _testField = value;
        } else{
            return InitResult.NoConfigElements("test-field");
        }

        return InitResult.Success;
    }
}

Supported types

Type
string
byte
int
long
double
Guid

Clone this wiki locally