| | 1 | | using System.Text; |
| | 2 | | using System.Text.Json; |
| | 3 | |
|
| | 4 | | namespace Common.Core.Classes; |
| | 5 | |
|
| | 6 | | /// <summary>Base class for Data Factory classes.</summary> |
| | 7 | | public abstract class DataFactoryBase |
| | 8 | | { |
| | 9 | | /// <summary>Default application settings file name.</summary> |
| | 10 | | public const string cConfigFile = "appsettings.json"; |
| | 11 | |
|
| | 12 | | /// <summary>Represents a pseudo-random number generator.</summary> |
| 1 | 13 | | protected static readonly Random sRandom = new(); |
| | 14 | |
|
| | 15 | | /// <summary>Serializes the Json data to a disk file.</summary> |
| | 16 | | /// <param name="obj">Object containing the data.</param> |
| | 17 | | /// <param name="path">Location for the Json data file.</param> |
| | 18 | | /// <param name="file">Name of the file. If not supplied the default name is used.</param> |
| | 19 | | /// <param name="options">Serialization options.</param> |
| | 20 | | /// <returns><see langword="true"/> if the data was serialized, otherwise <see langword="false"/> is returned.</return |
| | 21 | | protected static bool SerializeJson( object obj, string path, string file, JsonSerializerOptions options ) |
| 1 | 22 | | { |
| 1 | 23 | | return JsonHelper.Serialize( obj, Path.Combine( path, file ), options ); |
| 1 | 24 | | } |
| | 25 | |
|
| | 26 | | /// <summary>Reads a Json disk file and populates a factory object.</summary> |
| | 27 | | /// <typeparam name="T">Type of factory to populate.</typeparam> |
| | 28 | | /// <param name="path">Location of the data file.</param> |
| | 29 | | /// <param name="file">Name of the file.</param> |
| | 30 | | /// <param name="options">Json serializer options.</param> |
| | 31 | | /// <returns><see langword="null"/> is returned if the factory could not be populated.</returns> |
| | 32 | | protected static T? DeserializeJson<T>( string path, string file, JsonSerializerOptions options ) where T : DataFactor |
| 2 | 33 | | { |
| 2 | 34 | | var json = GetFileResource( path, file ); |
| 4 | 35 | | if( json is null ) { return null; } |
| 1 | 36 | | return JsonHelper.DeserializeJson<T>( ref json, options ); |
| 2 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary>Returns the Json from a disk file.</summary> |
| | 40 | | /// <param name="path">Location of the file.</param> |
| | 41 | | /// <param name="file">Name of the file.</param> |
| | 42 | | /// <returns><see langword="null"/> is returned if the resource could not be loaded.</returns> |
| | 43 | | protected static string? GetFileResource( string path, string file ) |
| 4 | 44 | | { |
| | 45 | | try |
| 4 | 46 | | { |
| 4 | 47 | | return File.ReadAllText( Path.Combine( path, file ), Encoding.ASCII ); |
| | 48 | | } |
| 6 | 49 | | catch( Exception ) { } |
| 2 | 50 | | return null; |
| 4 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary>Returns the requested number of data objects from a list.</summary> |
| | 54 | | /// <typeparam name="T">Data object class or interface.</typeparam> |
| | 55 | | /// <param name="data">Collection of data objects.</param> |
| | 56 | | /// <param name="count">Number of data objects to return.</param> |
| | 57 | | /// <returns>IList collection of data objects.</returns> |
| | 58 | | protected static IList<T> ReturnItems<T>( List<T> data, int count ) |
| 2 | 59 | | { |
| 2 | 60 | | int max = data.Count - count; |
| 2 | 61 | | if( max > 0 && count < data.Count ) |
| 1 | 62 | | { |
| 1 | 63 | | var idx = sRandom.Next( 1, max + 1 ); |
| 1 | 64 | | return data.GetRange( idx, count ); |
| | 65 | | } |
| 1 | 66 | | return data; |
| 2 | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary>Returns the start index for a requested number of data objects.</summary> |
| | 70 | | /// <param name="total">Total count of data objects.</param> |
| | 71 | | /// <param name="count">Number of data objects to return.</param> |
| | 72 | | /// <returns>The starting index.</returns> |
| | 73 | | protected static int GetStartIndex( int total, int count ) |
| 3 | 74 | | { |
| 3 | 75 | | int max = total - count; |
| 3 | 76 | | if( count > 0 && count <= total ) |
| 1 | 77 | | { |
| 1 | 78 | | return sRandom.Next( 1, max + 1 ); |
| | 79 | | } |
| 2 | 80 | | return 0; |
| 3 | 81 | | } |
| | 82 | | } |