我有一个问题,我希望我的处理程序使用从处理程序生成的数据:
- UpdateUserProfileImageCommandHandlerAuthorizeDecorator
- UpdateUserProfileImageCommandHandlerUploadDecorator
- UpdateUserProfileImageCommandHandler
我的问题是架构和性能。
UpdateUserCommandHandlerAuthorizeDecorator
调用存储库(实体框架)以授权用户。我有其他类似的装饰器应该使用和修改实体并将其发送到链上。
UpdateUserCommandHandler
应该只是将用户保存到数据库中。我目前必须进行另一个存储库调用并更新实体,而我本可以使用以前的装饰器处理实体。
我的问题是该命令只接受用户 ID 和一些要更新的属性。在我从 Authorize 装饰器中获取用户实体的情况下,我如何仍然在链上处理该实体?可以将该User
属性添加到命令中并进行处理吗?
代码:
public class UpdateUserProfileImageCommand : Command
{
public UpdateUserProfileImageCommand(Guid id, Stream image)
{
this.Id = id;
this.Image = image;
}
public Stream Image { get; set; }
public Uri ImageUri { get; set; }
}
public class UpdateUserProfileImageCommandHandlerAuthorizeDecorator : ICommandHandler<UpdateUserProfileImageCommand>
{
public void Handle(UpdateUserProfileImageCommand command)
{
// I would like to use this entity in `UpdateUserProfileImageCommandHandlerUploadDecorator`
var user = userRespository.Find(u => u.UserId == command.Id);
if(userCanModify(user, currentPrincipal))
{
decoratedHandler(command);
}
}
}
public class UpdateUserProfileImageCommandHandlerUploadDecorator : ICommandHandler<UpdateUserProfileImageCommand>
{
public void Handle(UpdateUserProfileImageCommand command)
{
// Instead of asking for this from the repository again, I'd like to reuse the entity from the previous decorator
var user = userRespository.Find(u => u.UserId == command.Id);
fileService.DeleteFile(user.ProfileImageUri);
var command.ImageUri = fileService.Upload(generatedUri, command.Image);
decoratedHandler(command);
}
}
public class UpdateUserProfileImageCommandHandler : ICommandHandler<UpdateUserProfileImageCommand>
{
public void Handle(UpdateUserProfileImageCommand command)
{
// Again I'm asking for the user...
var user = userRespository.Find(u => u.UserId == command.Id);
user.ProfileImageUri = command.ImageUri;
// I actually have this in a PostCommit Decorator.
unitOfWork.Save();
}
}