【发布时间】:2015-12-01 17:58:32
【问题描述】:
我正在尝试使用带有类似于 CQRS 中使用的命令模式的 Azure 消息队列。
这是一个示例命令:
public class SetZoneModeCommand : ICommand
{
public string GatewayId { get; set; }
public string ReceiverId { get; set; }
public int ChannelNumber { get; set; }
public HeatingMode Mode { get; set; }
}
这是它的处理程序
public class SetZoneModeCommandHandler : ICommandHandler<SetZoneModeCommand>
{
private readonly IDatabaseContext _databaseContext;
public SetZoneModeCommandHandler(IDatabaseContext databaseContext)
{
_databaseContext = databaseContext;
}
public RequestStatus Execute(SetZoneModeCommand command)
{
if (command == null)
{
throw new ArgumentNullException("command");
}
var result = new RequestStatus();
return result;
}
}
我在具有此配置的 Worker 角色中使用 Ninject:
_kernel.Bind(x => x.FromAssembliesMatching("Business.dll")
.SelectAllClasses()
.BindDefaultInterface());
这工作正常,并且正在注入依赖项。
我有一个使用 JSON 序列化的 QueuedCommand 对象,它被放置在 Azure 消息队列中:
public class QueuedCommand
{
public string ClassName { get; set; }
public object Command { get; set; }
public DateTime AddedOn { get; set; }
public int AddedByUserId { get; set; }
public int RetryCount { get; set; }
}
这是尝试反序列化 QueueCommand 并对其进行处理的(未优化的)代码:
var queuedCommand = (QueuedCommand)JsonConvert.DeserializeObject<QueuedCommand>(message.AsString);
var commandInterface = typeof(ICommand);
var commandType = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where (commandInterface.IsAssignableFrom(type)) && (commandInterface != type)
&& type.FullName == queuedCommand.ClassName
select type).FirstOrDefault();
var o = (JObject) queuedCommand.Command;
var command = (ICommand)o.ToObject(commandType);
var result = _commandDispatcher.Dispatch(command);
这一切都很好,如果我调试,传递给调度程序的命令对象是正确的类型并且填充了预期的值。
CommandDispatcher 应该为给定的 Command 找到 CommandHandler 的具体实现。我的问题是它不是,我收到一个关于 ICommandHandler 没有绑定的错误。
如果我将 ICommand 中的转换替换为 SetZoneModeCommand,那么它会按预期工作。这显然是不可接受的,我认为如果我有一个 Object 和一个完全限定的类名,它不会太难转换。
public interface ICommandDispatcher
{
/// <summary>
/// Dispatches a command to its handler
/// </summary>
/// <typeparam name="TParameter">Command Type</typeparam>
/// <param name="command">The command to be passed to the handler</param>
RequestStatus Dispatch<TParameter>(TParameter command) where TParameter : ICommand;
}
public CommandDispatcher(IKernel kernel)
{
if (kernel == null)
{
throw new ArgumentNullException("kernel");
}
_kernel = kernel;
}
public RequestStatus Dispatch<TParameter>(TParameter command) where TParameter : ICommand
{
var handler = _kernel.Get<ICommandHandler<TParameter>>();
return handler.Execute(command);
}
【问题讨论】: