用于以结构化格式导出和导入的 DTO 属于我的应用程序中的哪个位置?

发布于 2024-09-29 23:05:40 字数 168 浏览 0 评论 0原文

我们的用户需要能够以 CSV 格式导出数据、编辑一些记录并再次上传数据。数据不映射到实体,您可以说对象图被展平以适应基于 Excel 的工作流程。

现在这种情况发生在控制器中,因为我认为这些 DTO 类是视图模型。闻起来很臭,但我不知道如何解决这个问题。我可以/应该遵循某种模式吗?

谢谢!

Our users need to be able to export data in CSV format, edit some of the records, and upload the data again. The data does not map to entities, you could say that the object graph is flattened to fit in the Excel-based workflow.

Right now this happens in the controllers because I thought these DTO classes were view models. It smells but I don't have a clear idea how to fix that. Is there a pattern I could/should follow?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绅士风度i 2024-10-06 23:05:40

首先将此逻辑抽象为包含必要方法的接口。针对 CSV 格式实现此接口。在控制器的构造函数中传递接口。使用 DI 注入正确的实现。在控制器操作中调用接口上的方法。

如果您想直接从控制器操作返回 CSV,您可以编写自定义 ActionResult 类似于 CsvActionResult,它将获取模型并将其序列化为 CSV,以便在控制器操作中返回新的 CsvResult(someModel)

Start by abstract this logic into an interface containing the necessary method. Implement this interface against the CSV format. Pass the interface in the constructor of the controller. Use DI to inject the proper implementation. In the controller action call the method on the interface.

If you want to return CSV directly from your controller action you could write a custom ActionResult like CsvActionResult which will take the model and serialize it into CSV so that in your controller action you return new CsvResult(someModel).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文