POCO =普通旧CLR(或更好的:类)对象
DTO =数据传输对象
在这篇文章中有一个区别,但坦率地说,我读过的大多数博客都是用DTO的定义方式描述POCO的:DTO是简单的数据容器,用于在应用程序的层之间移动数据。
POCO和DTO是同一个东西吗?
POCO =普通旧CLR(或更好的:类)对象
DTO =数据传输对象
在这篇文章中有一个区别,但坦率地说,我读过的大多数博客都是用DTO的定义方式描述POCO的:DTO是简单的数据容器,用于在应用程序的层之间移动数据。
POCO和DTO是同一个东西吗?
当前回答
我已经在我的博客文章中阐述了我的立场,所以我在这里发表评论可能是多余的,但那篇文章的最后一段似乎总结了一些事情:
So, in conclusion, learn to love the POCO, and make sure you don’t spread any misinformation about it being the same thing as a DTO. DTOs are simple data containers used for moving data between the layers of an application. POCOs are full fledged business objects with the one requirement that they are Persistence Ignorant (no get or save methods). Lastly, if you haven’t checked out Jimmy Nilsson’s book yet, pick it up from your local university stacks. It has examples in C# and it’s a great read.
顺便说一句,帕特里克,我把POCO当作一篇生活方式的文章来读,我完全同意,这是一篇很棒的文章。这实际上是我推荐的吉米·尼尔森书中的一个章节。我不知道网上也有。他的书确实是我在POCO / DTO / Repository /和其他DDD开发实践方面找到的最好的信息来源。
其他回答
甚至不要称它们为dto。他们被称为模型....句号。模特从来没有行为。我不知道是谁想出了这个愚蠢的术语DTO,但它一定是一个。net的东西,这是我所能想到的。想想MVC中的视图模型,同样的东西,模型是用来在层与层之间传递状态的服务器端或线上传输,它们都是模型。属性与数据。这些是你传递的模型。模特,模特。就是这样。
我希望DTO这个愚蠢的术语能从我们的词汇表中消失。
DTO的一个主要用例是从web服务返回数据。在这种情况下,POCO和DTO是等价的。POCO中的任何行为在从web服务返回时都将被删除,因此它是否具有行为并不重要。
下面是一般规则:DTO=邪恶和过度工程软件的指示器。少= =好。“企业”模式已经摧毁了Java EE世界中许多人的大脑。请不要在。net领域重复这个错误。
我已经在我的博客文章中阐述了我的立场,所以我在这里发表评论可能是多余的,但那篇文章的最后一段似乎总结了一些事情:
So, in conclusion, learn to love the POCO, and make sure you don’t spread any misinformation about it being the same thing as a DTO. DTOs are simple data containers used for moving data between the layers of an application. POCOs are full fledged business objects with the one requirement that they are Persistence Ignorant (no get or save methods). Lastly, if you haven’t checked out Jimmy Nilsson’s book yet, pick it up from your local university stacks. It has examples in C# and it’s a great read.
顺便说一句,帕特里克,我把POCO当作一篇生活方式的文章来读,我完全同意,这是一篇很棒的文章。这实际上是我推荐的吉米·尼尔森书中的一个章节。我不知道网上也有。他的书确实是我在POCO / DTO / Repository /和其他DDD开发实践方面找到的最好的信息来源。
我认为DTO可以是POCO。DTO更多地是关于对象的使用,而POCO更多地是关于对象的风格(与体系结构概念分离)。
One example where a POCO is something different than DTO is when you're talking about POCO's inside your domain model/business logic model, which is a nice OO representation of your problem domain. You could use the POCO's throughout the whole application, but this could have some undesirable side effect such a knowledge leaks. DTO's are for instance used from the Service Layer which the UI communicates with, the DTO's are flat representation of the data, and are only used for providing the UI with data, and communicating changes back to the service layer. The service layer is in charge of mapping the DTO's both ways to the POCO domain objects.
Martin Fowler说这种方法是一条艰难的道路,只有在域层和用户界面之间存在严重不匹配的情况下才应该采用。