gpt4 book ai didi

dependency-injection - CQRS - 在命令中执行命令

转载 作者:行者123 更新时间:2023-12-04 08:21:55 25 4
gpt4 key购买 nike

我最近看到了一些代码场景,其中 CommandHandlers 被注入(inject) ICommandExecutor 以调用其他命令。所以命令中的命令。这对于注入(inject) IQuery 的一些 QueryHandler 也是如此。

public class UpdateCarDetailsCommandHandler : CommandHandler<UUpdateCarDetailsCommand>
{
private ICommandExecutor _command;

public UpdateCarDetailsCommandHandler (ICommandExecutor command)
{
_command = command;
}

public override void Execute(UpdateCarDetailsCommand command)
{
//do something with repository
_command.Execute(new CarColour())
}
}

这对我来说似乎不正确,因为 ICommandExecutor 在这种情况下将是组合根。只是想知道人们对此有何看法?

最佳答案

我说你对在其他命令和查询中使用命令和查询保持警惕是正确的。 当心做太多的抽象。

CQRS 中的 S代表Segregation .这清楚地暗示命令应该与其他命令分开,查询应该与其他查询分开。但是查询可以被命令使用吗?一如既往,这取决于。

乌迪大汉的article从 2009 年开始建议不要:

Since your queries are now being performed off of a separate data store than your master database, and there is no assumption that the data that’s being served is 100% up to date, you can easily add more instances of these stores without worrying that they don’t contain the exact same data.



迪诺埃斯波西托 recommends使用单独的项目:

Applying CQRS means you’ll use two distinct middle tiers. One tier takes care of commands that alter the system state. The other retrieves the data. You create a couple of class library projects—query stack and command stack—and reference both from the main Web server project.



我个人的看法是,您应该将这些标准的命令和查询处理程序视为 。整体抽象 .整体抽象是关注整个事务的抽象;在单个事务的范围内,它不能是比自身更大的事物的依赖关系。

相反,需要的是类似的抽象对,它们是可注入(inject)的策略,并且可以用横切关注点进行修饰。

例如。
public interface IDataCommandHandler<TCommand> where TCommand : IDataCommand
{
void Handle(TCommand command);
}

public interface IDataQueryHandler<TQuery, TResult> where TQuery : IDataQuery<TResult>
{
TResult Handle(TQuery query);
}

或者
public interface ICommandStrategyHandler<TCommand> where TCommand : ICommand
{
void Handle(TCommand command);
}

public interface IQueryStrategyHandler<TQuery, TResult> where TQuery : IQuery<TResult>
{
TResult Handle(TQuery query);
}

关于dependency-injection - CQRS - 在命令中执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31203506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com