c-em-env 1.0.1

dotnet add package c-em-env --version 1.0.1
NuGet\Install-Package c-em-env -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="c-em-env" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add c-em-env --version 1.0.1
#r "nuget: c-em-env, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install c-em-env as a Cake Addin
#addin nuget:?package=c-em-env&version=1.0.1

// Install c-em-env as a Cake Tool
#tool nuget:?package=c-em-env&version=1.0.1

C-EM-ENV

The c-em-env package simplifies dynamic variable access in C# applications, particularly for retrieving values from environment variables and configuration settings. It offers convenient functionality to handle .env files commonly used for configuration management.

Explanation

Variable, represents a dynamic variable that retrieves values from environment variables and configuration settings. It offers dynamic access to these values through the AsDynamic() method and dynamic property access.

Installation
  1. Add Package Reference:

    Ensure you have the required packages installed. Add the following package references to your project:

    dotnet add package c-em-env
    
  2. Usage:

    Register the Variable class in your application's service collection during startup:

    // Add the Variable class as a singleton service
    builder.Services.AddSingleton<IVariable, Variable>();
    
    // Initialize the Variable class with the application's configuration
    new Variable(builder.Configuration);
    
Constructor
/// Initializes a new instance of the <see cref="Variable"/> class.
/// <param name="config">The configuration.</param>
public Variable(IConfiguration config)
  • config: An instance of IConfiguration representing the application's configuration settings.
Methods
  1. AsDynamic()
/// Converts the variable to a dynamic object allowing dynamic access to environment variables and configuration settings.
/// <returns>A dynamic object representing the variable.</returns>
public dynamic AsDynamic()
  • Returns a dynamic object representing the environment variables and configuration settings.
  1. GetDynamicValue(string key)
/// Gets the value of a dynamic property identified by the specified key.
/// <param name="key">The key of the dynamic property.</param>
/// <returns>The value of the dynamic property.</returns>
public string? GetDynamicValue(string key)
  • key: The key of the dynamic property.
  • Returns the value of the dynamic property identified by the specified key.
Properties
  • this[string key]
/// Gets or sets the value of a dynamic property identified by the specified key.
/// <param name="key">The key of the dynamic property.</param>
/// <returns>The value of the dynamic property.</returns>
public dynamic this[string key]
  • key: The key of the dynamic property.
  • Provides access to the value of a dynamic property identified by the specified key.

Usage

// Example usage of Variable class
var variable = new Variable(configuration);
dynamic dynamicVariable = variable.AsDynamic();

// Accessing dynamic properties
string value = dynamicVariable.SomeKey;

// Getting dynamic property value
string value = variable.GetDynamicValue("SomeKey");

Notes

  • This class retrieves values from environment variables and configuration settings.
  • It provides dynamic access to these values using C#'s dynamic feature.
  • Error handling is included for cases where keys are null, whitespace, or not found in the configuration settings.
Features
  • Retrieve values from environment variables and configuration settings dynamically.
  • Supports dynamic property access for easy retrieval of values.
Important Note
  • .env File Required: Ensure you have an .env file present in your project directory with the necessary environment variables. The Variable class retrieves values from environment variables and requires an .env file to function correctly.

This guide assumes you're using .NET Core or .NET 5+ for your project, although this was created on .NET 8. Make sure to replace builder with your appropriate service provider registration mechanism if you're using a different framework or dependency injection container.

Author and Creator

Ethern Myth

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 103 3/12/2024
1.0.0 72 3/12/2024