I need to tidy up a system that creates a bunch of Programs. Each Program is defined in the following way:
- A program contains multiple modules
- A module contains multiple parameters
The C# looks like this for the Program:
public class Program : IProgram
{
/// <summary>Program name</summary>
public string ProgramName { get; set; }
/// <summary>Program name</summary>
public int ProgramId { get; set; }
/// <summary>Holds all the modules and its parameters</summary>
public Modules Modules { get; private set; } = new Modules();
public void Initialize(JToken programToken)
{
JToken modulesToken = programToken["MODULES"];
// Clear Modules
Modules.Clear();
foreach (var moduleToken in modulesToken)
{
IModule module = CreateModuleFromName((string)moduleToken["API"]);
if (module != null)
{
module.Initialize(moduleToken);
Modules.Add(module);
}
}
}
}
And for the Modules:
public class Modules : List<IModule>
{
public IModule this[string moduleName] => this.First(m => m.ModuleName== moduleName);
}
public class SomeModule : BaseModule
{
[DataMember]
public const string ModuleName= "SOME_MODULE";
[DataMember]
public const string EnableString = "Enable";
/// <summary>
/// Field representing the if this module is enabled
/// </summary>
[DataMember]
public IParameter Enable
{
get => Parameters.GetParameter(EnableString);
set => Parameters.Set(value);
}
}
I won't bore you with the Parameter class, which follows the same logic.
Now, the programs, modules and parameters get populated based on some Json file. The thing that bugs me here is that there is no inheritance between Programs -> Modules -> Parameters.
The coder decided to have the Program class parsing the Json at initialisation and then dynamically populate a list of modules.
Is this the correct approach? My limited understanding of the object world would think it is a bad approach (but maybe he had no choice in order to compose the List of Modules dynamically). If so, how would you correct it?
Please also note that Program, Module and Parameter classes are all independent assemblies. Which is also something I don't quite get. Should they not all be part of the same one?
program
andmodule
sounds like it violates basic principals of separation of concerns. Your model seems logical... I probably wouldn't have theprogram
type do any serialization, factories should be built to construct the types. – Trae Moore Dec 9 '19 at 14:28