【发布时间】:2013-11-22 08:00:12
【问题描述】:
我有一个问题,我希望我的处理程序使用从处理程序生成的数据:
- 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();
}
}
【问题讨论】:
-
您在问题中使用了“up the chain”两次。你这是什么意思?
-
@Steven,我认为装饰器类似于链接,1 个装饰器调用另一个装饰器,然后到达实际的命令处理程序。所以我希望数据从一个装饰器传递到另一个装饰器,直到处理程序
-
您能否更新您的问题并举例说明您在做什么?
-
@Steven 我写了一些代码,其中包含一些关于我正在尝试做的事情。
-
@ShawnMclean 如果您使用 NHibernate 之类的东西,那么您应该能够依靠二级缓存来避免重复的数据库调用来查找用户。您是否担心已验证存在的性能问题?
标签: c# .net dependency-injection decorator cqrs