Skip to main content
code update
Source Link
iSR5
  • 6.4k
  • 1
  • 9
  • 16
public class RecordManager { private static JsonConfiguation _jsonConfig; private class JsonConfiguation { public string RemoteFileName { get; set; }  public bool LoadDefault { get; set; } public bool Reload { get; set; } } public RecordManager(IConfiguration configuration, string localPath) { if(configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if(localPath?.Length == 0) { throw new ArgumentNullException(nameof(localPath)); } _localPath = localPath; _configuration = configuration; if(_jsonConfig == null) _jsonConfig = GetConfigValuesFromJson(); ChangeToken.OnChange(configuration.GetReloadToken, _ => ConfigChanged(), new object()); }  private JsonConfiguation GetConfigValuesFromJson() { string configValue = _configuration["configKey"];   public ifbool (string.IsNullOrWhiteSpace(configValue))LoadDefault { throw newget; ArgumentNullException(nameof(configValue));set; } var dcc = JsonConvert.DeserializeObject<ConsulConfig>(configValue); return new JsonConfiguation { RemoteFileName = dcc.remoteFileName, LoadDefault = bool.TryParse(dcc.loadDefaultFlag?.ToString(), outpublic bool loadDefaultFlag) ? loadDefaultFlag : false, Reload = bool.TryParse(dcc.reloadConfig?.ToString(), out bool reloadConfig) ? reloadConfig : false }; { get; set; }  private void ConfigChanged() { var configNew = GetConfigValuesFromJson();   // fallback in case if something happened unexpectedly. public bool ifHasNewerFile(_jsonConfig == null) { _jsonConfig = configNew; } var isFileSame = JsonConfiguation.RemoteFileName.Equals(configNew.RemoteFileName, StringComparer.InvariantCultureIgnoreCase); if(!configNew.LoadDefault && isFileSame) { // if both (the current downloaded and on the remote) are the same // just load the local config _records = GetConfigFromLocalFiles(); } else if(!configNew.LoadDefault && !isFileSame) { // they're different, you need to redownload the file before going to the next step. _records = GetConfigFromServer(); _jsonConfig = configNew; } else { //reload the default configuration _records = GetDefaultConfiguration(); _jsonConfig = configNew; } // if it requires to reload the configuration immediately // if not, it'll now reload the configuration, and it would be stored in this instance. if(configNew.ReloadjsonConfiguation) { return Save!RemoteFileName.Equals(jsonConfiguation.RemoteFileName, StringComparison.InvariantCultureIgnoreCase);  }  }  private IEnumerable<RecordHolder> GetDefaultConfiguration() { // get the default config files already present in default "Records" folder // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromServer() { // get the config files from the server // and return RecordHolder list back.   } private IEnumerable<RecordHolder> GetConfigFromLocalFiles() { // get the config files from the secondary location // and return RecordHolder list back. } privatepublic bool IsConfigFromServer()  { return=> !_jsonConfig.LoadDefault && !string.IsNullOrWhiteSpace(_jsonConfig.RemoteFileName); }  } public class RecordManager { private static JsonConfiguation _jsonConfig;   private class JsonConfiguation { public string RemoteFileName { get; set; } public bool LoadDefault { get; set; } public bool Reload { get; set; } }  public RecordManager(IConfiguration configuration, string localPath) { if(configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if(localPath?.Length == 0) { throw new ArgumentNullException(nameof(localPath)); } _localPath = localPath; _configuration = configuration; if(_jsonConfig == null) _jsonConfig = GetConfigValuesFromJson(); ChangeToken.OnChange(configuration.GetReloadToken, _ => ConfigChanged(), new object()); } private JsonConfiguation GetConfigValuesFromJson() { string configValue = _configuration["configKey"]; if (string.IsNullOrWhiteSpace(configValue)) { throw new ArgumentNullException(nameof(configValue)); } var dcc = JsonConvert.DeserializeObject<ConsulConfig>(configValue); return new JsonConfiguation { RemoteFileName = dcc.remoteFileName, LoadDefault = bool.TryParse(dcc.loadDefaultFlag?.ToString(), out bool loadDefaultFlag) ? loadDefaultFlag : false, Reload = bool.TryParse(dcc.reloadConfig?.ToString(), out bool reloadConfig) ? reloadConfig : false }; } private void ConfigChanged() { var configNew = GetConfigValuesFromJson();   // fallback in case if something happened unexpectedly. if (_jsonConfig == null) { _jsonConfig = configNew; }    var isFileSame = JsonConfiguation.RemoteFileName.Equals(configNew.RemoteFileName, StringComparer.InvariantCultureIgnoreCase); if(!configNew.LoadDefault && isFileSame) { // if both (the current downloaded and on the remote) are the same // just load the local config    _records = GetConfigFromLocalFiles();  } else if(!configNew.LoadDefault && !isFileSameIsConfigFromServer) { // they'reif both (the current downloaded and on the remote) are different, you   need to redownload  // Redownload the file before going to the next step.   // else just load the local config  _records = _jsonConfig.HasNewerFile(configNew) ? GetConfigFromServer() : GetConfigFromLocalFiles();   _jsonConfig = configNew; } else  { // here it will cover if the loadDefaultFlag is true or any other issue with the configuration (like missing values) // it will reload the default configuration   (as a reset switch).   _records = GetDefaultConfiguration();   _jsonConfig = configNew; }     // if it requires to reload the configuration immediately // if not, it'll now reload the configuration, and it would be stored in this instance. if (configNew.Reload) {   Save();  } } private IEnumerable<RecordHolder> GetDefaultConfiguration() { // get the default config files already present in default "Records" folder // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromServer() { // get the config files from the server // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromLocalFiles() { // get the config files from the secondary location // and return RecordHolder list back. }  private bool IsConfigFromServer() {  return !_jsonConfig.LoadDefault && !string.IsNullOrWhiteSpace(_jsonConfig.RemoteFileName); }  }  
public class RecordManager { private static JsonConfiguation _jsonConfig; private class JsonConfiguation { public string RemoteFileName { get; set; }  public bool LoadDefault { get; set; } public bool Reload { get; set; } } public RecordManager(IConfiguration configuration, string localPath) { if(configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if(localPath?.Length == 0) { throw new ArgumentNullException(nameof(localPath)); } _localPath = localPath; _configuration = configuration; if(_jsonConfig == null) _jsonConfig = GetConfigValuesFromJson(); ChangeToken.OnChange(configuration.GetReloadToken, _ => ConfigChanged(), new object()); }  private JsonConfiguation GetConfigValuesFromJson() { string configValue = _configuration["configKey"];    if (string.IsNullOrWhiteSpace(configValue)) { throw new ArgumentNullException(nameof(configValue)); } var dcc = JsonConvert.DeserializeObject<ConsulConfig>(configValue); return new JsonConfiguation { RemoteFileName = dcc.remoteFileName, LoadDefault = bool.TryParse(dcc.loadDefaultFlag?.ToString(), out bool loadDefaultFlag) ? loadDefaultFlag : false, Reload = bool.TryParse(dcc.reloadConfig?.ToString(), out bool reloadConfig) ? reloadConfig : false };  }  private void ConfigChanged() { var configNew = GetConfigValuesFromJson();   // fallback in case if something happened unexpectedly.  if(_jsonConfig == null) { _jsonConfig = configNew; } var isFileSame = JsonConfiguation.RemoteFileName.Equals(configNew.RemoteFileName, StringComparer.InvariantCultureIgnoreCase); if(!configNew.LoadDefault && isFileSame) { // if both (the current downloaded and on the remote) are the same // just load the local config _records = GetConfigFromLocalFiles(); } else if(!configNew.LoadDefault && !isFileSame) { // they're different, you need to redownload the file before going to the next step. _records = GetConfigFromServer(); _jsonConfig = configNew; } else { //reload the default configuration _records = GetDefaultConfiguration(); _jsonConfig = configNew; } // if it requires to reload the configuration immediately // if not, it'll now reload the configuration, and it would be stored in this instance. if(configNew.Reload) { Save();  }  }  private IEnumerable<RecordHolder> GetDefaultConfiguration() { // get the default config files already present in default "Records" folder // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromServer() { // get the config files from the server // and return RecordHolder list back.   } private IEnumerable<RecordHolder> GetConfigFromLocalFiles() { // get the config files from the secondary location // and return RecordHolder list back. } private bool IsConfigFromServer()  { return !_jsonConfig.LoadDefault && !string.IsNullOrWhiteSpace(_jsonConfig.RemoteFileName); }  } public class RecordManager { private static JsonConfiguation _jsonConfig;   private class JsonConfiguation { public string RemoteFileName { get; set; } public bool LoadDefault { get; set; } public bool Reload { get; set; } }  public RecordManager(IConfiguration configuration, string localPath) { if(configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if(localPath?.Length == 0) { throw new ArgumentNullException(nameof(localPath)); } _localPath = localPath; _configuration = configuration; if(_jsonConfig == null) _jsonConfig = GetConfigValuesFromJson(); ChangeToken.OnChange(configuration.GetReloadToken, _ => ConfigChanged(), new object()); } private JsonConfiguation GetConfigValuesFromJson() { string configValue = _configuration["configKey"]; if (string.IsNullOrWhiteSpace(configValue)) { throw new ArgumentNullException(nameof(configValue)); } var dcc = JsonConvert.DeserializeObject<ConsulConfig>(configValue); return new JsonConfiguation { RemoteFileName = dcc.remoteFileName, LoadDefault = bool.TryParse(dcc.loadDefaultFlag?.ToString(), out bool loadDefaultFlag) ? loadDefaultFlag : false, Reload = bool.TryParse(dcc.reloadConfig?.ToString(), out bool reloadConfig) ? reloadConfig : false }; } private void ConfigChanged() { var configNew = GetConfigValuesFromJson();   // fallback in case if something happened unexpectedly. if(_jsonConfig == null) { _jsonConfig = configNew; }    var isFileSame = JsonConfiguation.RemoteFileName.Equals(configNew.RemoteFileName, StringComparer.InvariantCultureIgnoreCase); if(!configNew.LoadDefault && isFileSame) { // if both (the current downloaded and on the remote) are the same // just load the local config    _records = GetConfigFromLocalFiles();  } else if(!configNew.LoadDefault && !isFileSame) { // they're different, you need to redownload the file before going to the next step. _records = GetConfigFromServer();   _jsonConfig = configNew; } else  { //reload the default configuration   _records = GetDefaultConfiguration();   _jsonConfig = configNew; }   // if it requires to reload the configuration immediately // if not, it'll now reload the configuration, and it would be stored in this instance. if(configNew.Reload) {   Save();  } } private IEnumerable<RecordHolder> GetDefaultConfiguration() { // get the default config files already present in default "Records" folder // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromServer() { // get the config files from the server // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromLocalFiles() { // get the config files from the secondary location // and return RecordHolder list back. }  private bool IsConfigFromServer() {  return !_jsonConfig.LoadDefault && !string.IsNullOrWhiteSpace(_jsonConfig.RemoteFileName); }  }  
public class RecordManager { private static JsonConfiguation _jsonConfig; private class JsonConfiguation { public string RemoteFileName { get; set; } public bool LoadDefault { get; set; } public bool Reload { get; set; } public bool HasNewerFile(JsonConfiguation jsonConfiguation) { return !RemoteFileName.Equals(jsonConfiguation.RemoteFileName, StringComparison.InvariantCultureIgnoreCase); } public bool IsConfigFromServer => !LoadDefault && !string.IsNullOrWhiteSpace(RemoteFileName); } public RecordManager(IConfiguration configuration, string localPath) { if(configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if(localPath?.Length == 0) { throw new ArgumentNullException(nameof(localPath)); } _localPath = localPath; _configuration = configuration; if(_jsonConfig == null) _jsonConfig = GetConfigValuesFromJson(); ChangeToken.OnChange(configuration.GetReloadToken, _ => ConfigChanged(), new object()); } private JsonConfiguation GetConfigValuesFromJson() { string configValue = _configuration["configKey"]; if (string.IsNullOrWhiteSpace(configValue)) { throw new ArgumentNullException(nameof(configValue)); } var dcc = JsonConvert.DeserializeObject<ConsulConfig>(configValue); return new JsonConfiguation { RemoteFileName = dcc.remoteFileName, LoadDefault = bool.TryParse(dcc.loadDefaultFlag?.ToString(), out bool loadDefaultFlag) ? loadDefaultFlag : false, Reload = bool.TryParse(dcc.reloadConfig?.ToString(), out bool reloadConfig) ? reloadConfig : false }; } private void ConfigChanged() { var configNew = GetConfigValuesFromJson(); // fallback in case if something happened unexpectedly. if (_jsonConfig == null) { _jsonConfig = configNew; } if(configNew.IsConfigFromServer) { // if both (the current downloaded and on the remote) are different,      // Redownload the file before going to the next step.   // else just load the local config  _records = _jsonConfig.HasNewerFile(configNew) ? GetConfigFromServer() : GetConfigFromLocalFiles(); _jsonConfig = configNew; } else { // here it will cover if the loadDefaultFlag is true or any other issue with the configuration (like missing values) // it will reload the default configuration (as a reset switch).   _records = GetDefaultConfiguration(); _jsonConfig = configNew; }    // if it requires to reload the configuration immediately // if not, it'll now reload the configuration, and it would be stored in this instance. if (configNew.Reload) { Save(); } } private IEnumerable<RecordHolder> GetDefaultConfiguration() { // get the default config files already present in default "Records" folder // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromServer() { // get the config files from the server // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromLocalFiles() { // get the config files from the secondary location // and return RecordHolder list back. } } 
added 4718 characters in body
Source Link
iSR5
  • 6.4k
  • 1
  • 9
  • 16
public class RecordManager { private static JsonConfiguation _jsonConfig; private class JsonConfiguation { public string RemoteFileName { get; set; } public bool LoadDefault { get; set; } public bool Reload { get; set; } } public RecordManager(IConfiguration configuration, string localPath) { if(configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if(localPath?.Length == 0) { throw new ArgumentNullException(nameof(localPath)); } _localPath = localPath; _configuration = configuration; if(_jsonConfig == null) _jsonConfig = GetConfigValuesFromJson(); ChangeToken.OnChange(configuration.GetReloadToken, _ => ConfigChanged(), new object()); } private JsonConfiguation GetConfigValuesFromJson() { string configValue = _configuration["configKey"]; if (string.IsNullOrWhiteSpace(configValue)) { throw new ArgumentNullException(nameof(configValue)); } var dcc = JsonConvert.DeserializeObject<ConsulConfig>(configValue); return new JsonConfiguation { RemoteFileName = _remoteFileNamedcc.remoteFileName, LoadDefault = bool.TryParse(dcc.loadDefaultFlag?.ToString(), out bool loadDefaultFlag) ? loadDefaultFlag : false, Reload = bool.TryParse(dcc.reloadConfig?.ToString(), out bool reloadConfig) ? reloadConfig : false }; } private void ConfigChanged() { var configNew = GetConfigValuesFromJson(); // fallback in case if something happened unexpectedly. if(_jsonConfig == null) { _jsonConfig = configNew; } var isFileSame = _jsonConfigJsonConfiguation.RemoteFileName.Equals(configNew.RemoteFileName, StringComparer.InvariantCultureIgnoreCase); if(!configNew.LoadDefault && isFileSame) { // if both (the current downloaded and on the remote) are the same // just load the local config _records = GetConfigFromLocalFiles(); } else if(!configNew.LoadDefault && !isFileSame) { // they're different, you need to redownload the file before going to the next step. _records = GetConfigFromServer(); _jsonConfig = configNew; } else { //reload the default configuration _records = GetDefaultConfiguration(); _jsonConfig = configNew; } // if it requires to reload the configuration immediately // if not, it'll now reload the configuration, and it would be stored in this instance. if(configNew.Reload) { Save(); } } private IEnumerable<RecordHolder> GetDefaultConfiguration() { // get the default config files already present in default "Records" folder // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromServer() { // get the config files from the server // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromLocalFiles() { // get the config files from the secondary location // and return RecordHolder list back. } private bool IsConfigFromServer() { return !_jsonConfig.LoadDefault && !string.IsNullOrWhiteSpace(_jsonConfig.RemoteFileName); } }  public class RecordManager { private static JsonConfiguation _jsonConfig; private class JsonConfiguation { public string RemoteFileName { get; set; } public bool LoadDefault { get; set; } public bool Reload { get; set; } } public RecordManager(IConfiguration configuration, string localPath) { if(configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if(localPath?.Length == 0) { throw new ArgumentNullException(nameof(localPath)); } _localPath = localPath; _configuration = configuration; if(_jsonConfig == null) _jsonConfig = GetConfigValuesFromJson(); ChangeToken.OnChange(configuration.GetReloadToken, _ => ConfigChanged(), new object()); } private JsonConfiguation GetConfigValuesFromJson() { string configValue = _configuration["configKey"]; if (string.IsNullOrWhiteSpace(configValue)) { throw new ArgumentNullException(nameof(configValue)); }   var dcc = JsonConvert.DeserializeObject<ConsulConfig>(configValue); return new JsonConfiguation { RemoteFileName = dcc.remoteFileName, LoadDefault = bool.TryParse(dcc.loadDefaultFlag?.ToString(), out bool loadDefaultFlag) ? loadDefaultFlag : false, Reload = bool.TryParse(dcc.reloadConfig?.ToString(), out bool reloadConfig) ? reloadConfig : false }; } private void ConfigChanged() { var configNew = GetConfigValuesFromJson(); // fallback in case if something happened unexpectedly. if(_jsonConfig == null) { _jsonConfig = configNew; } var isFileSame = JsonConfiguation.RemoteFileName.Equals(configNew.RemoteFileName, StringComparer.InvariantCultureIgnoreCase); if(!configNew.LoadDefault && isFileSame) { // if both (the current downloaded and on the remote) are the same // just load the local config _records = GetConfigFromLocalFiles(); } else if(!configNew.LoadDefault && !isFileSame) { // they're different, you need to redownload the file before going to the next step. _records = GetConfigFromServer(); _jsonConfig = configNew; } else { //reload the default configuration _records = GetDefaultConfiguration(); _jsonConfig = configNew; } // if it requires to reload the configuration immediately // if not, it'll now reload the configuration, and it would be stored in this instance. if(configNew.Reload) { Save(); } } private IEnumerable<RecordHolder> GetDefaultConfiguration() { // get the default config files already present in default "Records" folder // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromServer() { // get the config files from the server // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromLocalFiles() { // get the config files from the secondary location // and return RecordHolder list back. } private bool IsConfigFromServer() { return !_jsonConfig.LoadDefault && !string.IsNullOrWhiteSpace(_jsonConfig.RemoteFileName); } }  
public class RecordManager { private static JsonConfiguation _jsonConfig; private class JsonConfiguation { public string RemoteFileName { get; set; } public bool LoadDefault { get; set; } public bool Reload { get; set; } } public RecordManager(IConfiguration configuration, string localPath) { if(configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if(localPath?.Length == 0) { throw new ArgumentNullException(nameof(localPath)); } _localPath = localPath; _configuration = configuration; if(_jsonConfig == null) _jsonConfig = GetConfigValuesFromJson(); ChangeToken.OnChange(configuration.GetReloadToken, _ => ConfigChanged(), new object()); } private JsonConfiguation GetConfigValuesFromJson() { string configValue = _configuration["configKey"]; if (string.IsNullOrWhiteSpace(configValue)) { throw new ArgumentNullException(nameof(configValue)); } var dcc = JsonConvert.DeserializeObject<ConsulConfig>(configValue); return new JsonConfiguation { RemoteFileName = _remoteFileName, LoadDefault = bool.TryParse(dcc.loadDefaultFlag?.ToString(), out bool loadDefaultFlag) ? loadDefaultFlag : false, Reload = bool.TryParse(dcc.reloadConfig?.ToString(), out bool reloadConfig) ? reloadConfig : false }; } private void ConfigChanged() { var configNew = GetConfigValuesFromJson(); // fallback in case if something happened unexpectedly. if(_jsonConfig == null) { _jsonConfig = configNew; } var isFileSame = _jsonConfig.RemoteFileName.Equals(configNew.RemoteFileName, StringComparer.InvariantCultureIgnoreCase); if(!configNew.LoadDefault && isFileSame) { // if both (the current downloaded and on the remote) are the same // just load the local config _records = GetConfigFromLocalFiles(); } else if(!configNew.LoadDefault && !isFileSame) { // they're different, you need to redownload the file before going to the next step. _records = GetConfigFromServer(); _jsonConfig = configNew; } else { //reload the default configuration _records = GetDefaultConfiguration(); _jsonConfig = configNew; } // if it requires to reload the configuration immediately // if not, it'll now reload the configuration, and it would be stored in this instance. if(configNew.Reload) { Save(); } } private IEnumerable<RecordHolder> GetDefaultConfiguration() { // get the default config files already present in default "Records" folder // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromServer() { // get the config files from the server // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromLocalFiles() { // get the config files from the secondary location // and return RecordHolder list back. } private bool IsConfigFromServer() { return !_jsonConfig.LoadDefault && !string.IsNullOrWhiteSpace(_jsonConfig.RemoteFileName); } } 
public class RecordManager { private static JsonConfiguation _jsonConfig; private class JsonConfiguation { public string RemoteFileName { get; set; } public bool LoadDefault { get; set; } public bool Reload { get; set; } } public RecordManager(IConfiguration configuration, string localPath) { if(configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if(localPath?.Length == 0) { throw new ArgumentNullException(nameof(localPath)); } _localPath = localPath; _configuration = configuration; if(_jsonConfig == null) _jsonConfig = GetConfigValuesFromJson(); ChangeToken.OnChange(configuration.GetReloadToken, _ => ConfigChanged(), new object()); } private JsonConfiguation GetConfigValuesFromJson() { string configValue = _configuration["configKey"]; if (string.IsNullOrWhiteSpace(configValue)) { throw new ArgumentNullException(nameof(configValue)); } var dcc = JsonConvert.DeserializeObject<ConsulConfig>(configValue); return new JsonConfiguation { RemoteFileName = dcc.remoteFileName, LoadDefault = bool.TryParse(dcc.loadDefaultFlag?.ToString(), out bool loadDefaultFlag) ? loadDefaultFlag : false, Reload = bool.TryParse(dcc.reloadConfig?.ToString(), out bool reloadConfig) ? reloadConfig : false }; } private void ConfigChanged() { var configNew = GetConfigValuesFromJson(); // fallback in case if something happened unexpectedly. if(_jsonConfig == null) { _jsonConfig = configNew; } var isFileSame = JsonConfiguation.RemoteFileName.Equals(configNew.RemoteFileName, StringComparer.InvariantCultureIgnoreCase); if(!configNew.LoadDefault && isFileSame) { // if both (the current downloaded and on the remote) are the same // just load the local config _records = GetConfigFromLocalFiles(); } else if(!configNew.LoadDefault && !isFileSame) { // they're different, you need to redownload the file before going to the next step. _records = GetConfigFromServer(); _jsonConfig = configNew; } else { //reload the default configuration _records = GetDefaultConfiguration(); _jsonConfig = configNew; } // if it requires to reload the configuration immediately // if not, it'll now reload the configuration, and it would be stored in this instance. if(configNew.Reload) { Save(); } } private IEnumerable<RecordHolder> GetDefaultConfiguration() { // get the default config files already present in default "Records" folder // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromServer() { // get the config files from the server // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromLocalFiles() { // get the config files from the secondary location // and return RecordHolder list back. } private bool IsConfigFromServer() { return !_jsonConfig.LoadDefault && !string.IsNullOrWhiteSpace(_jsonConfig.RemoteFileName); } }  public class RecordManager { private static JsonConfiguation _jsonConfig; private class JsonConfiguation { public string RemoteFileName { get; set; } public bool LoadDefault { get; set; } public bool Reload { get; set; } } public RecordManager(IConfiguration configuration, string localPath) { if(configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if(localPath?.Length == 0) { throw new ArgumentNullException(nameof(localPath)); } _localPath = localPath; _configuration = configuration; if(_jsonConfig == null) _jsonConfig = GetConfigValuesFromJson(); ChangeToken.OnChange(configuration.GetReloadToken, _ => ConfigChanged(), new object()); } private JsonConfiguation GetConfigValuesFromJson() { string configValue = _configuration["configKey"]; if (string.IsNullOrWhiteSpace(configValue)) { throw new ArgumentNullException(nameof(configValue)); }   var dcc = JsonConvert.DeserializeObject<ConsulConfig>(configValue); return new JsonConfiguation { RemoteFileName = dcc.remoteFileName, LoadDefault = bool.TryParse(dcc.loadDefaultFlag?.ToString(), out bool loadDefaultFlag) ? loadDefaultFlag : false, Reload = bool.TryParse(dcc.reloadConfig?.ToString(), out bool reloadConfig) ? reloadConfig : false }; } private void ConfigChanged() { var configNew = GetConfigValuesFromJson(); // fallback in case if something happened unexpectedly. if(_jsonConfig == null) { _jsonConfig = configNew; } var isFileSame = JsonConfiguation.RemoteFileName.Equals(configNew.RemoteFileName, StringComparer.InvariantCultureIgnoreCase); if(!configNew.LoadDefault && isFileSame) { // if both (the current downloaded and on the remote) are the same // just load the local config _records = GetConfigFromLocalFiles(); } else if(!configNew.LoadDefault && !isFileSame) { // they're different, you need to redownload the file before going to the next step. _records = GetConfigFromServer(); _jsonConfig = configNew; } else { //reload the default configuration _records = GetDefaultConfiguration(); _jsonConfig = configNew; } // if it requires to reload the configuration immediately // if not, it'll now reload the configuration, and it would be stored in this instance. if(configNew.Reload) { Save(); } } private IEnumerable<RecordHolder> GetDefaultConfiguration() { // get the default config files already present in default "Records" folder // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromServer() { // get the config files from the server // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromLocalFiles() { // get the config files from the secondary location // and return RecordHolder list back. } private bool IsConfigFromServer() { return !_jsonConfig.LoadDefault && !string.IsNullOrWhiteSpace(_jsonConfig.RemoteFileName); } }  
added 8 characters in body
Source Link
iSR5
  • 6.4k
  • 1
  • 9
  • 16
public class RecordManager { private static JsonConfiguation _jsonConfig; private class JsonConfiguation { public string RemoteFileName { get; set; } public bool LoadDefault { get; set; } public bool Reload { get; set; } } public RecordManager(IConfiguration configuration, string localPath) { if(configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if(localPath?.Length == 0) { throw new ArgumentNullException(nameof(localPath)); } _localPath = localPath; _configuration = configuration; if(_jsonConfig == null) _jsonConfig = GetConfigValuesGetConfigValuesFromJson(); ChangeToken.OnChange(configuration.GetReloadToken, _ => ConfigChanged(), new object()); } private JsonConfiguation GetConfigValuesFromJson() { string configValue = _configuration["configKey"]; if (string.IsNullOrWhiteSpace(configValue)) { throw new ArgumentNullException(nameof(configValue)); } var dcc = JsonConvert.DeserializeObject<ConsulConfig>(configValue); return new JsonConfiguation { RemoteFileName = _remoteFileName, LoadDefault = bool.TryParse(dcc.loadDefaultFlag?.ToString(), out bool loadDefaultFlag) ? loadDefaultFlag : false, Reload = bool.TryParse(dcc.reloadConfig?.ToString(), out bool reloadConfig) ? reloadConfig : false }; } private void ConfigChanged() { var configNew = GetConfigValuesFromJson(); // fallback in case if something happened unexpectedly. if(_jsonConfig == null) { _jsonConfig = configNew; } var isFileSame = _jsonConfig.RemoteFileName.Equals(configNew.RemoteFileName, StringComparer.InvariantCultureIgnoreCase); if(!configNew.LoadDefault && isFileSame) { // if both (the current downloaded and on the remote) are the same // just load the local config _records = GetConfigFromLocalFiles(); } else if(!configNew.LoadDefault && !isFileSame) { // they're different, you need to redownload the file before going to the next step. _records = GetConfigFromServer(); _jsonConfig = configNew; } else { //reload the default configuration _records = GetDefaultConfiguration(); _jsonConfig = configNew; } // if it requires to reload the configuration immediately // if not, it'll now reload the configuration, and it would be stored in this instance. if(configNew.Reload) { Save(); } } private IEnumerable<RecordHolder> GetDefaultConfiguration() { // get the default config files already present in default "Records" folder // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromServer() { // get the config files from the server // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromLocalFiles() { // get the config files from the secondary location // and return RecordHolder list back. } private bool IsConfigFromServer() { return !_jsonConfig.LoadDefault && !string.IsNullOrWhiteSpace(_jsonConfig.RemoteFileName); } } 
public class RecordManager { private static JsonConfiguation _jsonConfig; private class JsonConfiguation { public string RemoteFileName { get; set; } public bool LoadDefault { get; set; } public bool Reload { get; set; } } public RecordManager(IConfiguration configuration, string localPath) { if(configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if(localPath?.Length == 0) { throw new ArgumentNullException(nameof(localPath)); } _localPath = localPath; _configuration = configuration; if(_jsonConfig == null) _jsonConfig = GetConfigValues(); ChangeToken.OnChange(configuration.GetReloadToken, _ => ConfigChanged(), new object()); } private JsonConfiguation GetConfigValuesFromJson() { string configValue = _configuration["configKey"]; if (string.IsNullOrWhiteSpace(configValue)) { throw new ArgumentNullException(nameof(configValue)); } var dcc = JsonConvert.DeserializeObject<ConsulConfig>(configValue); return new JsonConfiguation { RemoteFileName = _remoteFileName, LoadDefault = bool.TryParse(dcc.loadDefaultFlag?.ToString(), out bool loadDefaultFlag) ? loadDefaultFlag : false, Reload = bool.TryParse(dcc.reloadConfig?.ToString(), out bool reloadConfig) ? reloadConfig : false }; } private void ConfigChanged() { var configNew = GetConfigValuesFromJson(); // fallback in case if something happened unexpectedly. if(_jsonConfig == null) { _jsonConfig = configNew; } var isFileSame = _jsonConfig.RemoteFileName.Equals(configNew.RemoteFileName, StringComparer.InvariantCultureIgnoreCase); if(!configNew.LoadDefault && isFileSame) { // if both (the current downloaded and on the remote) are the same // just load the local config _records = GetConfigFromLocalFiles(); } else if(!configNew.LoadDefault && !isFileSame) { // they're different, you need to redownload the file before going to the next step. _records = GetConfigFromServer(); _jsonConfig = configNew; } else { //reload the default configuration _records = GetDefaultConfiguration(); _jsonConfig = configNew; } // if it requires to reload the configuration immediately // if not, it'll now reload the configuration, and it would be stored in this instance. if(configNew.Reload) { Save(); } } private IEnumerable<RecordHolder> GetDefaultConfiguration() { // get the default config files already present in default "Records" folder // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromServer() { // get the config files from the server // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromLocalFiles() { // get the config files from the secondary location // and return RecordHolder list back. } private bool IsConfigFromServer() { return !_jsonConfig.LoadDefault && !string.IsNullOrWhiteSpace(_jsonConfig.RemoteFileName); } } 
public class RecordManager { private static JsonConfiguation _jsonConfig; private class JsonConfiguation { public string RemoteFileName { get; set; } public bool LoadDefault { get; set; } public bool Reload { get; set; } } public RecordManager(IConfiguration configuration, string localPath) { if(configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if(localPath?.Length == 0) { throw new ArgumentNullException(nameof(localPath)); } _localPath = localPath; _configuration = configuration; if(_jsonConfig == null) _jsonConfig = GetConfigValuesFromJson(); ChangeToken.OnChange(configuration.GetReloadToken, _ => ConfigChanged(), new object()); } private JsonConfiguation GetConfigValuesFromJson() { string configValue = _configuration["configKey"]; if (string.IsNullOrWhiteSpace(configValue)) { throw new ArgumentNullException(nameof(configValue)); } var dcc = JsonConvert.DeserializeObject<ConsulConfig>(configValue); return new JsonConfiguation { RemoteFileName = _remoteFileName, LoadDefault = bool.TryParse(dcc.loadDefaultFlag?.ToString(), out bool loadDefaultFlag) ? loadDefaultFlag : false, Reload = bool.TryParse(dcc.reloadConfig?.ToString(), out bool reloadConfig) ? reloadConfig : false }; } private void ConfigChanged() { var configNew = GetConfigValuesFromJson(); // fallback in case if something happened unexpectedly. if(_jsonConfig == null) { _jsonConfig = configNew; } var isFileSame = _jsonConfig.RemoteFileName.Equals(configNew.RemoteFileName, StringComparer.InvariantCultureIgnoreCase); if(!configNew.LoadDefault && isFileSame) { // if both (the current downloaded and on the remote) are the same // just load the local config _records = GetConfigFromLocalFiles(); } else if(!configNew.LoadDefault && !isFileSame) { // they're different, you need to redownload the file before going to the next step. _records = GetConfigFromServer(); _jsonConfig = configNew; } else { //reload the default configuration _records = GetDefaultConfiguration(); _jsonConfig = configNew; } // if it requires to reload the configuration immediately // if not, it'll now reload the configuration, and it would be stored in this instance. if(configNew.Reload) { Save(); } } private IEnumerable<RecordHolder> GetDefaultConfiguration() { // get the default config files already present in default "Records" folder // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromServer() { // get the config files from the server // and return RecordHolder list back. } private IEnumerable<RecordHolder> GetConfigFromLocalFiles() { // get the config files from the secondary location // and return RecordHolder list back. } private bool IsConfigFromServer() { return !_jsonConfig.LoadDefault && !string.IsNullOrWhiteSpace(_jsonConfig.RemoteFileName); } } 
Source Link
iSR5
  • 6.4k
  • 1
  • 9
  • 16
Loading