获取命令模式中已执行方法的结果 [英] Get result of executed method in Command Pattern
问题描述
当前,我正在尝试实现事务脚本模式(正是Martin Fowler所描述的 通过在一个简单的测试项目中使用命令模式),一切正常,问题是当从ICommand接口继承的具体类中执行指定的方法时,我不知道如何获得结果.
Currently I'm trying to implement Transaction Script pattern (Exactly how Martin Fowler described by using Command Pattern) in a simple test project, everything just work fine, the problem is where I don't know how to get result(s) when specified method executed in concrete class which is inherited from ICommand interface.
让我们为您展示一些代码,以阐明我的功能. 我有一个简单的CalculateSalaryCommand类,该类是从ICommand接口继承的
Let's show you some code to clarify what functionality I have. I've a simple CalculateSalaryCommand class which inherited from 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);
}
}
和一个名为CalculateSalaryTS的简单事务脚本类
and a simple Transaction Script class named CalculateSalaryTS
public class CalculateSalaryTS {
public void CalculateSalary(int _hour, int _salaryPerHour) {
Result = _hour * _salaryPerHour;
}
}
如您所见,我将的实例传递给具体的命令类,然后在Execute方法内部执行该实例的操作.好吧,一切看起来都不错.但是有一个问题,我不能返回应该为整数的已执行方法的结果.
为了解决这个问题,我决定向交易脚本层添加一些代码,每个交易应从通用的ITransactionResult
接口继承该代码,如下所示:
as you can see I pass the instance of to concrete command class, then inside the Execute method I execute a operations from that instance. Well, everything just look good. but there is a problem I can't return the result of executed method which is should be a integer.
To handle this problem, I decided to add some code to Transaction Script layer which each transaction should inherit from a generic ITransactionResult
interface, which is look like following:
public interface ITransactionResult<TResult>
{
TResult Result { get; set; }
}
然后CalculateSalaryTS类变为:
Then CalculateSalaryTS class became like this :
public class CalculateSalaryTS : ITransactionResult<Int32> {
public void CalculateSalary(int _hour, int _salaryPerHour) {
Result = _hour * _salaryPerHour;
}
public int Result { get; set; }
}
用法:
var script = new CalculateSalaryTS();
var command = new CalculateSalaryCommand(script, 10, 20);
command.Execute();
Console.WriteLine("Salary is {0}", script.Result);
我知道这种方式有其自身的局限性,但是直到您给我另一个想法来处理这种情况之前,我别无选择.
I know this way has its own limitation but I don't have any choice till you give me another idea to handle this situation.
谢谢.
推荐答案
如果绝对需要在命令执行后立即获取结果,则可以将结果存储在命令对象中:
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);
这篇关于获取命令模式中已执行方法的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!