Added a new Helper class#65
Conversation
|
@rogusdev Could you check this PR? Thanks. |
|
I see where you are going with this, and why. Honestly, I am a bit concerned about introducing something so different from how env vars are normally collected. Generally my personal recommendation is to have a class that collects all such values that you need in your application, and pass a version of that around as a mock/fake, instead of mocking out the env vars functionality of the language itself. That said, I can understand. So, my main questions is: are you sure about the index access? It seems like the get methods are the real way people should access such things. |
|
@rogusdev The indexer is just a simpler way to access the environment variable. It would be interesting that the library has a feature like this, it's like in PHP for example, you access the environment variables in this way: |
| { | ||
| get | ||
| { | ||
| if (instance == null) |
There was a problem hiding this comment.
This is not thread safe. You should read up on how to make a singleton thread safe, if you really must have a singleton at all -- I strongly discourage it, personally.
There was a problem hiding this comment.
@rogusdev To avoid complications, I have removed the property.
With this new functionality the
valueof the environment variable can be retrieved through the indexer:The advantage of this is that you can use dependency injection, either manually or by using a service container such as Microsoft.Extensions.DependencyInjection.
For example:
The
IEnvReaderinterface also has some auxiliary methods to retrieve the value of an environment variable in a specific format (integer, double, etc.) and warns the client when it does not find the variable. It is true that currently the library already has this functionality but it does not warn the client when it does not find an environment variable.Methods that do not start with
Try(asGetBoolValue, etc.) throw an exception when the environment variable is not found. Methods that start withTry(asTryGetBoolValue), however, returnfalseif the variable is not found, so the client has two options for handling the error.