【问题标题】:ReactiveCommands - Are the following solutions equivalent?ReactiveCommands - 以下解决方案是否等效?
【发布时间】:2020-09-07 07:49:31
【问题描述】:

解决方案 A)

public ReactiveCommand<Unit, Unit> CommandDoStuff => this._commandDoStuff;
private readonly ReactiveCommand<Unit, Unit> _commandDoStuff;

this._commandDoStuff = ReactiveCommand.CreateFromTask(async () =>
{
  //pass in parameters directly, do stuff, handle output maybe with an interaction, return Unit;
  var result = await DoStuff(this.Parameter);
  SharedInteraction.Handle(result);
  return;
});

this._commandDoStuff.Execute().Subscribe();

解决方案 B)

public ReactiveCommand<T, T> CommandDoStuff => this._commandDoStuff;
private readonly ReactiveCommand<T, T> _commandDoStuff;

this._commandDoStuff = ReactiveCommand.CreateFromTask(async (T) =>
{
  //do stuff, return T
  var result = await DoStuff(T);
  return result;
});

this._commandDoStuff.Execute(T).Subscribe(t =>
{
  //handle output maybe with an interaction
  SharedInteraction.Handle(t);
  return;
});

我知道这些是蹩脚的问题,但这是为了避免提出基于意见的问题......

这两种解决方案都“有效”吗?

两种解决方案是否等效?如果不是,有什么区别?

一种解决方案的性能更高吗?

一种解决方案是否比另一种解决方案更频繁地出现错误?

一种解决方案是否比另一种更遵循软件工程范式?

你明白我在问什么! :)

【问题讨论】:

    标签: .net architecture software-design reactiveui


    【解决方案1】:

    所以要避免基于意见的答案。我会尽我所能坚持我所知道的。

    Interactions 是关于控制流的。我的 ViewModel 中有一些功能正在发生,而我的 ViewModel 具有控制权。我需要用户输入才能完成此功能。我将显示一个Interaction 以从用户那里获取该信息。

    所以要回答,这取决于您需要在哪里显示信息。

    当您订阅ReactiveCommand 的执行时,您实际上是在订阅它的执行。这意味着该命令将执行,然后您在Subscribe() 中的代码将执行。如果您期望Interaction 显示ReactiveCommand 完成所需的信息,这几乎没有什么价值。

    也就是说,如果您只是通过Interaction 提醒用户该命令已完成,那么这可能不是问题。

    性能将取决于操作是什么。

    您是否有特定的范式,或者只是在寻找难以捉摸的“最佳实践”?

    一般来说,Stack Overflow 不喜欢这些开放式问题,因为没有具体的答案。

    【讨论】:

    • 太棒了!这正是我一直在寻找的,对我来说似乎很具体,谢谢!
    • 请随意将答案标记为正确答案。
    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    相关资源
    最近更新 更多