I am creating some software that runs on a machine that performs multiple tests on a product that is built at my company. I am also trying to improve my coding and I have been recently researching design patterns and seeing how I can apply them to my code.
The part I am currently looking at is the actual test sequence. At the moment my code looks similar to this
public class TaskItem
{
public string TaskName {get;set;}
public DateTime StartTaskDateTime {get;set;}
public DateTime EndTaskDateTime {get;set;}
public Funct<bool> TaskFunction {get;set;}
public bool Execute()
{
StartDateTime = DateTime.Now;
var taskResult = TaskFunction.Invoke();
EndDateTime = DateTime.Now;
return taskResult;
}
}
Then my class that builds the sequence and runs it is like this
List<TaskItem> TestTaskItems = new List<TaskItem>();
public void BuildTestSquence()
{
TestTaskItems.Add(new TaskItem()
{
TaskFunction = () =>
{
// Do some Stuff
}
};
// ... add more task items
}
public bool RunTestSequnce()
{
foreach(var taskItem in TestTaskItems)
{
if(!taskItem.Execute())
{
return false;
}
}
return true;
}
As the BuildTestSquence
method could be adding anywhere between 1 task to 100 tasks, you can image that the method could have a lot of code in it.
In my actual code I current have 400 lines of code in that method.
I am just wondering if what I have done is really the right way of doing it.
I was thinking maybe the Chain of Responsibility
might be an idea, and if it is then I am not too sure on the best way to implement it. From what I have seen so far, most examples show that you pass in an object and each chain does something with this object before deciding whether to pass it on or not. I won't be passing in any object.