Thanks to ASP.NET Core dependency injection, there's no need to follow the Singleton pattern; any class will do.
What I want to do is to have a class that will store a value that'll be used by another methods inside the app. A specific class is in charge of updating this value. What I'm concerned is the problem of a thread reading the value while the specific class updates it, given that I haven't done enough concurrency to feel confident.
So I came up with this. Is it correct for what I want it to do?
public class DynamicValueStore
{
private readonly object _lock_obj;
private string _value;
public string Value
{
get
{
lock (_lock_obj)
{
return _value;
}
}
}
public DynamicValueStore()
{
_lock_obj = new object();
_value = string.Empty;
}
public void UpdateValue(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentNullException(nameof(value));
}
lock (_lock_obj)
{
_value = value;
}
}
}
```