Get the result of the executed method in the Command Pattern

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.

+11
c # design-patterns command architecture command-pattern
source share
2 answers

If you absolutely need to get the result right after the command is executed, you can save 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); 
+22
source share

I think you are looking for a command template that can return a 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); } } 
+6
source share

All Articles