简体   繁体   English

CQRS 和 DDD:文件上传

[英]CQRS and DDD: File uploads

I'm new to the concept of DDD and CQRS and can't find a final solution how to upload images, or files in general, in a clean way.我是 DDD 和 CQRS 概念的新手,无法找到如何以干净的方式上传图像或一般文件的最终解决方案。

Imagine the following scenario: In an online portal there is a support request formular where a file (image in specific) could be attached to.想象一下以下场景:在在线门户中有一个支持请求公式,其中可以附加文件(特定图像)。 The posted data will raise a CreateSupportRequestCommand .发布的数据将引发CreateSupportRequestCommand Then the required aggregates will be loaded and changed.然后将加载和更改所需的聚合。

I have three ideas to solve this, but I'm not very satisfied with them.我有三个想法来解决这个问题,但我对它们不是很满意。

Way 1:方式一:
1. Post all data including the image (multipart) in a single request 1. 在单个请求中发布包括图像(多部分)在内的所有数据
2. Create a FileUploadCommand , which is returning the FileUploadId . 2. 创建一个FileUploadCommand ,它返回FileUploadId
3. After that create a CreateSupportRequestCommand and pass the FileUploadId with the root data in the constructor. 3. 之后创建一个CreateSupportRequestCommand并在构造函数中传递带有根数据的FileUploadId
Drawback: A single request will trigger two commands.缺点:单个请求将触发两个命令。 In terms of CQRS one user interaction should be only one command.就 CQRS 而言,一次用户交互应该只是一个命令。

Way 2:方式二:
1. Post the image to a seperate endpoint, create a temporary file and return the id or a file handle. 1. 将图像发布到单独的端点,创建一个临时文件并返回 id 或文件句柄。
2. Post the formular with the attached tempfile id. 2. 发布带有附加临时文件 ID 的公式。
3. Invoke the CreateSupportRequestCommand with all root data including a file handle which points to the physical file. 3. 使用包括指向物理文件的文件句柄在内的所有根数据调用CreateSupportRequestCommand
4. Inside the command persist the tempfile into a FileUpload aggregate (by FileUploadRepository ) then 4. 在命令中将临时文件持久化到FileUpload聚合中(通过FileUploadRepository )然后
5. Create the SupportRequest aggregate, assign the FileUploadId and persist. 5. 创建SupportRequest聚合,分配 FileUploadId 并保留。
Drawback: I handle 2 aggregates in the same command.缺点:我在同一个命令中处理 2 个聚合。 Creating a support request is not responsible for uploading the file.创建支持请求不负责上传文件。

Way 3: 1. Post the image to a seperate endpoint, create a temporary file and return the id or a file handle.方式三: 1. 将图像发布到单独的端点,创建一个临时文件并返回 id 或文件句柄。
2. Post the formular with the attached tempfile id. 2. 发布带有附加临时文件 ID 的公式。
3. Invoke the CreateSupportRequestCommand with all root data including a file handle which points to the physical file. 3. 使用包括指向物理文件的文件句柄在内的所有根数据调用CreateSupportRequestCommand
4. Only persist the root data to the SupportRequest aggregate. 4. 仅将根数据持久化到SupportRequest聚合中。 Raise a SupportRequestCreatedEvent and attach the file handle. SupportRequestCreatedEvent并附加文件句柄。
5. Inside the event process and assign the file handle. 5.在事件进程内部并分配文件句柄。
Drawback: The SupportRequestCreatedEvent should not really care about a file handle.缺点: SupportRequestCreatedEvent不应该真正关心文件句柄。

Is there a better way to solve this?有没有更好的方法来解决这个问题?

I do not think handling File upload is a Domain Concern.我不认为处理文件上传是域问题。 The file metadata like FileContentId may be part of your domain but not the actual file upload. FileContentId等文件元数据可能属于您的域,但不是实际文件上传的一部分。 I would perform the file operation before the CommandHandler is executed.我会在执行CommandHandler之前执行文件操作。 Probably in a middleware or perhaps before queing up the Command onto the message bus.可能在中间件中,或者可能在将Command排队到消息总线之前。

CreateSupportRequestCommandHandler would then only be invoking an operation like CreateSupportRequest on your aggrerate (say SupportRequest ). CreateSupportRequestCommandHandler会那么只能调用一个operation就像CreateSupportRequest您aggrerate(说SupportRequest )。 Within that CreateSupportRequest method you will have all your business rule pretaining to the operation.在该CreateSupportRequest方法中,您将拥有适用于该操作的所有业务规则。 SupportRequest then eventually would be saved in your repository. SupportRequest最终将保存在您的存储库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM