将部分填充的 DTO 映射到域对象

发布于 2024-09-17 00:03:45 字数 484 浏览 2 评论 0原文

我在业务层和表示层之间使用 DTO,并在服务中使用一些映射代码来将 DTO <-> 转换为 DTO。域对象。我当前允许 PL 部分填充 DTO 并将其发送到更新服务,该服务仅更新关联 DO 上已更改的属性。

处理部分填充的 DTO 中的不可空(值)类型的常用方法是什么?对于可空类型,我只检查 DTO 值是否为空,如果不是,则在 DO 上设置相应的值。但不可为 null 的值将始终包含一个值,该值可能已由 PL 设置,也可能未设置。

我可以:

  • 在 DTO 中使用自由格式字符串作为名义上的值类型属性,并转换为值类型或从值类型转换
  • 使 PL 调用服务方法来更新值属性,而不是通过 DTO 传递它们
  • 强制 PL 始终将完全填充的 DTO 发送到更新服务

这些似乎都不理想:是否有我缺少的选项?或者我是从错误的角度来处理这个问题的?

如果相关的话,我正在使用 C# 4、WCF 和 ASP.NET MVC

I'm using DTOs between my business and presentation layers and have some mapping code in the service that converts DTO <-> domain object. I'm currently allowing the PL to partially populate a DTO and send it to an Update service, which updates only the changed properties on the associated DO.

What's the usual way of dealing with non-nullable (value) types in partially populated DTOs? For nullable types I just check if the DTO value is null, and if not, set the corresponding value on the DO. But the non-nullables will always contain a value, which may or may not have been set by the PL.

I could:

  • use free-form strings in the DTO for the notionally value-typed properties and convert to/from the value type
  • make the PL call a service method to update the value properties rather than pass them via the DTO
  • force the PL to always send a fully populated DTO to the Update service

None of those seems ideal: is there an option I'm missing? Or am I approaching this problem from the wrong angle?

If it's relevant, I'm using C# 4, WCF and ASP.NET MVC

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

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

发布评论

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

评论(2

孤檠 2024-09-24 00:03:45

您能否提供有关您提到的不可为空值类型的更多信息?您是否知道可以在 DTO 中使用的可空类型

Could you give more information about the non-nullable value types that you mentioned? Are you aware of Nullable Types that can be used in your DTO?

过期情话 2024-09-24 00:03:45

一种方法是将更改的属性列表传递给更新服务。这可以像使用一个整数一样简单,其中每个位指示某个属性或属性索引或名称的数组等。您还可以使 DTO 进行自我跟踪,即每个 DTO 将维护已更改的属性。

我通常不喜欢这样的部分更新 - 如果允许这些更新,那么在我看来,创建一个复合 DTO(将属性划分为子对象拥有的属性组)是有意义的,其中客户端能够在组级别进行更新(即所有必须填充组内的属性)。如果每个属性级别都需要控制更新,那么使用 PropertyBag(名称/索引 - 值对字典)类型的 DTO 更有意义。

One way is to pass the list of properties changed to the Update Service. This can be as simple as having an integer where each bit indicating some property OR array of property indices or names etc. You can also make your DTO's self tracking in a sense that each DTO will maintain what properties have been changed.

I typically does not prefer such partial updates - if these to be allowed then IMO, it would make sense to create a composite DTO (divide properties into group of properties owned by child objects) where client has ability to update at group level (i.e. all properties within group must be populated). If control to update is needed at each property level then it make more sense to use PropertyBag (dictionary of name/index - value pair) kind of DTO.

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