I am currently trying to implement a Script transaction template (exactly as described in the description ) "rel =" noreferrer "> Command Pattern) in a simple test project, everything just works fine, the problem is that I don’t know how to get the result (s) when the specified method is executed in a specific class that is inherited from the ICommand interface .
Show you some code to clarify what functionality I have. I have a simple CalculateSalaryCommand class that is inherited from the ICommand interface
public class CalculateSalaryCommand : ICommand { 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 void Execute() { _salaryTs.CalculateSalary(_hour, _salaryPerHour); } }
and a simple Script transaction class called CalculateSalaryTS
public class CalculateSalaryTS { public void CalculateSalary(int _hour, int _salaryPerHour) { Result = _hour * _salaryPerHour; } }
as you can see, I am passing an instance of a particular class of commands, then inside the Execute method, I execute operations from that instance. Well, everything just looks good. but there is a problem, I can not return the result of the executed method, which should be an integer. To deal with this problem, I decided to add some code to the Transaction Script layer, which each transaction should inherit from the common ITransactionResult interface, which looks like this:
public interface ITransactionResult<TResult> { TResult Result { get; set; } }
Then the CalculateSalaryTS class became as follows:
public class CalculateSalaryTS : ITransactionResult<Int32> { public void CalculateSalary(int _hour, int _salaryPerHour) { Result = _hour * _salaryPerHour; } public int Result { get; set; } }
Using:
var script = new CalculateSalaryTS(); var command = new CalculateSalaryCommand(script, 10, 20); command.Execute(); Console.WriteLine("Salary is {0}", script.Result);
I know that this method has its limitations, but I have no choice until you give me another idea to deal with this situation.
Thanks in advance.