Git Product home page Git Product logo

envmapper's Introduction

EnvMapper

EnvMapper Build EnvMapper Tests License: MIT NuGet version

A .NET library to map environment variables to a C# object. This is useful when deploying to containerized environments where environment variables may be injected before starting up.

Installation

Available on NuGet

Visual Studio:

PM> Install-Package EnvMapper

.NET Core CLI:

dotnet add package EnvMapper

Usage

Load env file

MapConfiguration() will automatically search for environment variables and map them to a class decorated with System.Runtime.Serialization.DataMember attributes. This supports overriding using the Name and IsRequired properties of the DataMemberAttribute.

var config = EnvMapper.Env.MapConfiguration<TType>();

If properties that are marked as required are not found or are of the wrong type (ie a string instead of an int), a EnvMapperException will be thrown, which contains a list of error properties.

Example

Given the following example class:

[DataContract]
public class TestConfiguration
{
    [DataMember(Name="Foo", IsRequired = true)]
    public string? Foo { get; set; }

    [DataMember]
    public string? Bar { get; set; }

    [DataMember(Name="Override")]
    public string? OtherField { get; set; }
}

The following can be used to access the variables that are present as environment variables:

var config = EnvMapper.Env.MapConfiguration<TestConfiguration>();
Console.WriteLine($"Foo={config.Foo}");//This must be present
Console.WriteLine($"Bar={config.Bar}");//This may be present
Console.WriteLine($"Otherfield={config.OtherField}");//This may be present

Note: in the above example the variables that will be checked are Foo, Bar, and Override. If there is an environment variable OtherField, the reader will not check that as the DataMember signified on the variable should be Override.

envmapper's People

Contributors

richardd012 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.