Git Product home page Git Product logo

simpleconfig's Introduction

ExistsForAll

ExistsForAll.SimpleSetting (previously SimpleConfig)

Installation

Install-Package ExistForAll.SimpleSettings

Table of Content

1.Introduction
2.Getting started
3.Building the collection
4.Building Config Interfaces
5.DefaultValue
6.Build Section Binders
7.Extending SimpleConfig

Introduction - or why SimpleConfig was created

With the release of Asp.Net Core and .Net Core Microsoft has introduced IOptions<>. While IOptions<> is a great concept it lacks in implementation. IOptions<> provide a way to insert parameters into your app dynamically.

For example you have an email service that requires some URL to the email server.

public class EmailSender
{
    public EmailSender(string emailServiceUrl, ... )
    {

    }
}

All of the good IOC containers out there will tell you that injecting a string into a service is a bad idea. The best of them won't let you do it.

IOptions<> to the rescue, with IOptions<> you can request the option class that can provide the string you want provided from any data store you want (json file, database and so on).

public class EmailSender
{
    public EmailSender(IOption<EmailProviderConfiguratation> configuration, ... ) { }
}

BUT IOptions<> is not the way to do this.

Why you ask ?

  1. As Uncle Bob said, your application must be independent from frameworks this the application code takes an unnecessary dependency on a framework abstraction, this is a violation of DIP.
  2. In order to inject IOption<SomeClass> SomeClass have to be a concrete class and not an interface.
  3. To use IOption<SomeClass> you must call services.Configure<SomeClass> in the Setup class, this is not scale-able in any way and the last thing we want to do is to manually configure each configuration class.
  4. Registering IOption<> in any other DI container different from Microsoft new DI container won't be a ball park.

For better understanding you can read this explenation from the SimpleInjecor docs.

TL;DR - or what SimpleConfig does?

Remember the IOption<EmailProviderConfiguratation> configuration?

what if we could build an interface like so

public interface IEmailServiceConfig
{
    [DefaultValue("SomeUrl")]
    string ServiceUrl {get; set;}
}

and use it like so

public class EmailSender : IEmailSender
{
   public EmailSender(IEmailServiceConfig emailServiceConfig, ... ) { }

   public void SendEmail(...)
   {
        Send(emailServiceConfig.ServiceUrl, ...);
    }
}

simpleconfig's People

Contributors

guy-lud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  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.