获取命令模式中执行方法的结果 [英] Get result of executed method in Command Pattern

查看:18
本文介绍了获取命令模式中执行方法的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在尝试实现事务脚本模式(正是 Martin Fowler 描述的方式通过在一个简单的测试项目中使用 Command Pattern),一切正常,问题是在继承自 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆