Last active
January 24, 2019 01:19
-
-
Save kenzauros/127be6447fb8d5a135a1d6f39f948795 to your computer and use it in GitHub Desktop.
DotNetEnv を使った .NET アプリでの環境変数管理用ヘルパークラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.CompilerServices; | |
/// <summary> | |
/// 環境変数を管理します。 | |
/// </summary> | |
public static class Env | |
{ | |
/// <summary> | |
/// デフォルトの .env ファイルのファイル名 | |
/// </summary> | |
public const string DEFAULT_ENV_FILENAME = ".env"; | |
/// <summary> | |
/// 環境変数を .env ファイルから読み込みます。 | |
/// </summary> | |
/// <param name="path"></param> | |
public static void Load(string path = null) | |
{ | |
DotNetEnv.Env.Load(path ?? DEFAULT_ENV_FILENAME); | |
} | |
#region 環境変数プロパティからの呼び出し用プロキシメソッド | |
public static string GetString([CallerMemberName] string key = "") => DotNetEnv.Env.GetString(key); | |
public static int GetInt([CallerMemberName] string key = "") => DotNetEnv.Env.GetInt(key); | |
public static bool GetBool([CallerMemberName] string key = "") => DotNetEnv.Env.GetBool(key); | |
public static double GetDouble([CallerMemberName] string key = "") => DotNetEnv.Env.GetDouble(key); | |
#endregion | |
#region 環境変数プロパティ | |
// サンプル | |
public static string FILE_PATH => GetString(); | |
public static int EXPIRATION_PERIOD => GetInt(); | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment