POCO 与 DTO:可以部分水合域对象吗?

发布于 2024-07-19 03:30:56 字数 350 浏览 4 评论 0原文

通常需要在 UI 上以各种方式显示域对象; 列表、搜索结果、查看和编辑页面以及页眉、页脚和弹出窗口。 通常,域对象有多个不同的“视图”,每个视图都显示不同的字段。

大多数建议似乎是在需要子集或超集时使用 DTO 来获取数据。 维护 DTO 会产生大量开销。 简单地填充每个场景所需的域对象的属性是不是一个坏方法? 例如,您可以使用配置文件来说明应包含哪些属性,例如:

service.GetDomainObjects(int listID, Profile.ListProfile); service.GetDomainObjects(string searchParam, Profile.SearchProfile);

Its often a requirement to have a domain object displayed in various ways on the UI; lists, search results, view and edit pages, as well as in headers, footers and popups. Typically you have several different "views" of the domain object, each with different fields displayed.

Most advice seems to be to use a DTO to get the data when you require a subset or superset. There is a lot of overhead in maintaining DTOs. Is it a bad approach to simply fill the properties of the domain object required for each scenario. For instance you might use a profile to say what properties should be included, eg:

service.GetDomainObjects(int listID, Profile.ListProfile);
service.GetDomainObjects(string searchParam, Profile.SearchProfile);

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

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

发布评论

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

评论(1

白衬杉格子梦 2024-07-26 03:30:56

对我来说,这归结为您想要的开销,要么您将有一组不同的类来表示您的 DTO,要么您将有一组方法,每个方法都返回相同的域对象,但是不同的领域都被“水合”了。

为了帮助做出决定,我会问几个问题:

  • 给整个对象加水的开销是多少? 增加的复杂性(DTO 或部分水合物体)真的值得吗?
  • 还有其他人会使用您的代码吗? 不要让人们与部分水合的对象混淆,当人们来维护你的代码时,DTO 可能会更清楚。

我个人对 DTO 有一点偏爱,因为我觉得系统的长期维护会更容易。 如果您是一个单人乐队,或者这是一个一次性的应用程序,我完全可以理解不想引入一堆额外的类来扰乱您的代码。

For me what this comes down to is where you want the overhead to be, either you are going to have a set of different classes to represent your DTOs, or you are going to have a set of methods that each return the same domain object but with different fields being 'hydrated'.

A couple of questions I'd ask to help make the decision are:

  • what is the overhead in hydrating the entire object? Is the added complexity (of DTOs or partially hydrated objects) really worthwhile?
  • is anyone else going to be using your code? You don't to confuse people with paritally hydrated objects, DTOs might be more clear when people come to maintain your code.

I have a slight personal preference for DTOs as I feel the long term maintainance of your system will be easier. If your a one man band, or this is a one off throw away app, I can totally understand not wanting to introduce a bunch of extra classes that will clutter your code.

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