Get result of executed method in Command Pattern
If you absolutely need to get the result immediately after command execution, you could store the result in the command object:
public interface ICommandWithResult<T> : ICommand
{
T Result { get; }
}
public class CalculateSalaryCommand : ICommandWithResult<int>
{
public int Result { get; private set; }
// ...
public void Execute()
{
_salaryTs.CalculateSalary(_hour, _salaryPerHour);
this.Result = _salaryTs.Result;
}
}
// Usage:
var command = new CalculateSalaryCommand(new CalculateSalaryTS(), 10, 20);
command.Execute();
Console.WriteLine("Salary is {0}", command.Result);
I think you are looking for a command pattern which is capable of returning result.
so,
interface ICommand<TResult>
{
TResult Execute();
}
public class CalculateSalaryCommand : ICommand<int>
{
private readonly CalculateSalaryTS _salaryTs;
private readonly int _hour;
private readonly int _salaryPerHour;
public CalculateSalaryCommand(CalculateSalaryTS salaryTs, int hour, int salaryPerHour)
{
_salaryTs = salaryTs;
_hour = hour;
_salaryPerHour = salaryPerHour;
}
public int Execute()
{
return _salaryTs.CalculateSalary(_hour, _salaryPerHour);
}
}