| | | 1 | | namespace Configuration.Helper; |
| | | 2 | | |
| | | 3 | | /// <summary>Settings Store factory implementation using a Windows disk file.</summary> |
| | | 4 | | public sealed class FileSettingsStore : SettingsStoreBase |
| | | 5 | | { |
| | | 6 | | #region Factory Pattern |
| | | 7 | | |
| | | 8 | | /// <inheritdoc /> |
| | 40 | 9 | | private FileSettingsStore() |
| | 80 | 10 | | { } |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Synchronous factory method to create an ISettingsStore object from a File System configuration file. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="configFile">Full path and file name of the configuration file. |
| | | 16 | | /// When no value is passed a new object will be created without a location.</param> |
| | | 17 | | /// <returns>FileSettingsStore object implementing the ISettingStore interface.</returns> |
| | | 18 | | /// <exception cref="ArgumentNullException">Thrown if the parameter is <see langword="null"/>.</exception> |
| | | 19 | | public static ISettingsStore Create( string configFile = "" ) |
| | 38 | 20 | | { |
| | | 21 | | // Check the required parameter is supplied |
| | | 22 | | const string cMethod = @"FileSettingsStore.Create"; |
| | 40 | 23 | | if( null == configFile ) { throw new ArgumentNullException( nameof( configFile ), cMethod ); } |
| | | 24 | | |
| | | 25 | | // Check the parameter has a value |
| | 37 | 26 | | configFile = configFile.Trim(); |
| | 37 | 27 | | FileSettingsStore retValue = new(); |
| | 37 | 28 | | if( configFile.Length == 0 ) |
| | 14 | 29 | | { |
| | 14 | 30 | | retValue.IsInitialized = true; |
| | 14 | 31 | | return retValue; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | // Create a Settings Store using a disk file name |
| | 23 | 35 | | FileInfo? fileInfo = IOHelper.GetFileInfo( configFile ); |
| | 92 | 36 | | if( fileInfo is not null ) { retValue.Initialize( fileInfo ); } |
| | | 37 | | |
| | 23 | 38 | | return retValue; |
| | 37 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Asynchronous factory method to create an ISettingsStore object from a File System configuration file. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="configFile">Configuration file containing the settings.</param> |
| | | 45 | | /// <returns>FileSettingsStore object implementing the ISettingStore interface.</returns> |
| | | 46 | | /// <exception cref="ArgumentNullException">Thrown if the parameter is <see langword="null"/>.</exception> |
| | | 47 | | public static async Task<ISettingsStore> CreateAsync( FileInfo configFile ) |
| | 4 | 48 | | { |
| | | 49 | | // Check the required parameter is supplied |
| | | 50 | | const string cMethod = @"FileSettingsStore.CreateAsync"; |
| | 6 | 51 | | if( null == configFile ) { throw new ArgumentNullException( nameof( configFile ), cMethod ); } |
| | | 52 | | |
| | | 53 | | // Create a Settings Store using a disk file |
| | 3 | 54 | | FileSettingsStore retValue = new(); |
| | 3 | 55 | | await retValue.InitializeAsync( configFile ); |
| | | 56 | | |
| | 3 | 57 | | return retValue; |
| | 3 | 58 | | } |
| | | 59 | | |
| | | 60 | | #endregion |
| | | 61 | | |
| | | 62 | | #region Private Methods |
| | | 63 | | |
| | | 64 | | private void Initialize( FileInfo configFile ) |
| | 23 | 65 | | { |
| | | 66 | | // Return an uninitialized Setting Store if the file does not exist |
| | 25 | 67 | | if( !configFile.Exists ) { return; } |
| | | 68 | | |
| | | 69 | | // Store the configuration file extension |
| | 22 | 70 | | fileExtension = IOHelper.GetExtension( configFile.Name ).ToLower(); |
| | | 71 | | |
| | 22 | 72 | | Source = IOHelper.CheckIfLocal( configFile.FullName ); |
| | | 73 | | try |
| | 22 | 74 | | { |
| | | 75 | | // Initialize the Setting Store |
| | 22 | 76 | | using( MemoryStream stream = new() ) |
| | 22 | 77 | | { |
| | | 78 | | // Copy the stream and place in a stream reader |
| | 22 | 79 | | configFile.OpenRead().CopyTo( stream ); |
| | 22 | 80 | | stream.Position = 0; |
| | 22 | 81 | | using StreamReader reader = new( stream ); |
| | | 82 | | // Initialize the setting store from the stream reader |
| | 22 | 83 | | string config = reader.ReadToEnd(); |
| | 22 | 84 | | LoadFromStream( ref config ); |
| | 20 | 85 | | } |
| | 20 | 86 | | Location = configFile.FullName; |
| | 20 | 87 | | IsInitialized = true; |
| | 20 | 88 | | } |
| | 6 | 89 | | catch( Exception ) { } // Do nothing - IsInitialized will be False |
| | 23 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary>Initializes a Setting Store asynchronously using a configuration disk file.</summary> |
| | | 93 | | /// <param name="configFile">Configuration file containing the settings.</param> |
| | | 94 | | private async Task InitializeAsync( FileInfo configFile ) |
| | 3 | 95 | | { |
| | | 96 | | // Return an uninitialized Setting Store if the file does not exist |
| | 5 | 97 | | if( !configFile.Exists ) { return; } |
| | | 98 | | |
| | | 99 | | // Store the configuration file extension |
| | 2 | 100 | | fileExtension = IOHelper.GetExtension( configFile.Name ).ToLower(); |
| | | 101 | | |
| | 2 | 102 | | Source = IOHelper.CheckIfLocal( configFile.FullName ); |
| | | 103 | | try |
| | 2 | 104 | | { |
| | | 105 | | // Initialize the Setting Store |
| | 2 | 106 | | using( MemoryStream stream = new() ) |
| | 2 | 107 | | { |
| | | 108 | | // Copy the stream and place in a stream reader |
| | 2 | 109 | | await configFile.OpenRead().CopyToAsync( stream ); |
| | 2 | 110 | | stream.Position = 0; |
| | 2 | 111 | | using StreamReader reader = new( stream ); |
| | | 112 | | // Initialize the setting store from the stream reader |
| | 2 | 113 | | string config = await reader.ReadToEndAsync(); |
| | 2 | 114 | | LoadFromStream( ref config ); |
| | 1 | 115 | | } |
| | 1 | 116 | | Location = configFile.FullName; |
| | 1 | 117 | | IsInitialized = true; |
| | 1 | 118 | | } |
| | 3 | 119 | | catch( Exception ) { } // Do nothing - IsInitialized will be False |
| | 3 | 120 | | } |
| | | 121 | | |
| | | 122 | | #endregion |
| | | 123 | | } |