現在、コマンド同期の処理から離れて、後で処理できるようにメッセージ バスに配置していますが、コマンドを 1 つずつ入力するのではなく、コマンドを読み込もうとすると、実際の型を取り戻すのに問題があります。
これが私がこれまでに持っているもので、うまく機能しているようです
コマンドディスパッチャー
public class CommandDispatcher : ICommandDispatcher
{
private readonly IBus _commandBus;
public CommandDispatcher(IBus commandBus)
{
_commandBus = commandBus;
}
public void Dispatch<TCommand>(TCommand command) where TCommand : ICommand
{
var messageType = typeof(TCommand);
_commandBus.Publish(messageType, command);
}
コマンドのサブスクライブ
bus.Subscribe<CommandOne>("key", HandleCommand);
bus.Subscribe<CommandTwo>("key", HandleCommand);
メッセージの処理
private void HandleCommand<TCommand>(TCommand command) where TCommand : ICommand
{
var handler = _container.Resolve<ICommandHandler<TCommand>>();
handler.Handle(command);
}
私は基本的に慣例に従ってメッセージを解決したいので、ここに移動したいので、各コマンドタイプを入力する必要はありませんが、タイプを元に戻すのに問題があります
var commandAssembly = Assembly.GetAssembly(typeof(ICommand));
var commands = commandAssembly.GetTypes().Where(x => typeof(ICommand).IsAssignableFrom(x));
foreach (var command in commands)
{
bus.Subscribe(command, "key", x =>
{
HandleCommand(x);
});
}
x は、Handlecommand メソッドに渡すことができない単なるオブジェクトです。
コンテナーには autofac を使用し、バスには easynetq を使用しています。