Skip to content

Extension library to provide some Environment functionality, for example for using with Docker-apps.

License

Notifications You must be signed in to change notification settings

bluewalk/DotNetEnvironmentExtensions

Repository files navigation

DotNetEnvironmentExtensions

Extension library to provide some Environment functionality, for example for using with Docker-apps.

Build status

Publish NuGet

NuGet Package

This appender is available as a NuGet package.

Run Install-Package Net.Bluewalk.DotNetEnvironmentExtensions in the Package Manager Console or search for "DotNetEnvironmentExtensions" in your IDE's package management plug-in.

Usage

The following functionalities are available:

Method Parameters Result Extends
GetEnvironmentVariable<T> name, optional defaultValue T
GetEnvironmentVariable type, name, optional defaultValue object
FromEnvironment autoCreateInstances void object
FromEnvironment autoCreateInstances void Type

Use the following attribute to decorate properties with environment variables: EnvironmentVariable

Examples

Read USE_THIS as boolean from the environment

var useThis = EnvironmentExtensions.GetEnvironmentVariable<bool>("USE_THIS", false);

OR

var useThis = EnvironmentExtensions.GetEnvironmentVariable(typeof(bool), "USE_THIS", false);

Fill object from environment

Parameter autoCreateInstances can be used to define if properties that are non-simple types need to me instantiated automatically. Note: these classes need to have a parameter-less constructor

Example class

public class MySettings
{
  [EnvironmentVariable(Name = "SOME_STRING", Default = "ABCdef")]
  public string SomeString { get; set; }
  [EnvironmentVariable(Name = "ENABLE_SMTHNG", Default = true)]
  public bool EnableSomething { get; set; }  
  [EnvironmentVariable(Name = "MY_INT_VAL", Default = 1234)]
  public int MyIntValue { get; set; }
  public SubSettings SubSettings { get; set; }
}

public class SubSettings
{
  [EnvironmentVariable(Name = "ANOTHER_STRING")]
  public string AnotherString { get; set; }
  [EnvironmentVariable(Name = "ANOTHER_INT_VAL", Default = -1)]
  public int AnotherIntValue { get; set; }
}

Read from environment

var mySettings = typeof(MySettings).FromEnvironment();

OR

var mySettings = new MySettings();
mySettings.FromEnvironment();

About

Extension library to provide some Environment functionality, for example for using with Docker-apps.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages