| | 1 | | namespace Songhay.Models; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Defines the in-memory storage |
| | 5 | | /// and Program access to <see cref="IActivity"/> types. |
| | 6 | | /// </summary> |
| | 7 | | public abstract class ActivitiesGetter |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Initializes a new instance of the <see cref="ActivitiesGetter" /> class. |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="args">The arguments.</param> |
| 7 | 13 | | public ActivitiesGetter(string[] args) |
| 7 | 14 | | { |
| 7 | 15 | | _defaultActivityName = ToActivityName(args); |
| 5 | 16 | | Args = new ProgramArgs(ToActivityArgs(args)); |
| 5 | 17 | | } |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Gets the <see cref="ProgramArgs"/>. |
| | 21 | | /// </summary> |
| 4 | 22 | | public ProgramArgs Args { get; } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Gets the <see cref="IActivity"/>. |
| | 26 | | /// </summary> |
| 4 | 27 | | public virtual IActivity? GetActivity() => _activities.GetActivity(_defaultActivityName); |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Gets the <see cref="IActivity"/>. |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="activityName">Name of the <see cref="IActivity"/>.</param> |
| 1 | 33 | | public virtual IActivity? GetActivity(string activityName) => _activities.GetActivity(activityName); |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Loads the activities into memory, lazily. |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="activities">The activities.</param> |
| 5 | 39 | | public virtual void LoadActivities(Dictionary<string, Lazy<IActivity?>>? activities) => _activities = activities; |
| | 40 | |
|
| | 41 | | static string[] ToActivityArgs(string[] args) |
| 5 | 42 | | { |
| 5 | 43 | | return args.Length < 2 ? Enumerable.Empty<string>().ToArray() : args.Skip(1).ToArray(); |
| 5 | 44 | | } |
| | 45 | |
|
| | 46 | | static string ToActivityName(string[] args) |
| 7 | 47 | | { |
| 7 | 48 | | args.ThrowWhenNullOrEmpty(); |
| | 49 | |
|
| 5 | 50 | | return args.First(); |
| 5 | 51 | | } |
| | 52 | |
|
| | 53 | | Dictionary<string, Lazy<IActivity?>>? _activities; |
| | 54 | | readonly string _defaultActivityName; |
| | 55 | | } |