POCO =普通旧CLR(或更好的:类)对象

DTO =数据传输对象

在这篇文章中有一个区别,但坦率地说,我读过的大多数博客都是用DTO的定义方式描述POCO的:DTO是简单的数据容器,用于在应用程序的层之间移动数据。

POCO和DTO是同一个东西吗?


当前回答

DTO objects are used to deserialize data into objects from different sources. Those objects are NOT your Model (POCO) objects. You need to transform those objects into your Model (POCO) objects. The transformation is mostly a copy operation. You can fill those POCO objects directly from the source if its an internal source, but its not adviceable if its an external source. External sources have API's with descriptions of the Schema they use. Its much easier then to load the request data in an DTO and after that transform those in your POCO's. Yes its an extra step, but with a reason. The rule is to load the data from your source in an object. It can be JSON, XML whatever. When loaded then transform that data in what you need in your model. So most of times the DTO is an object image of the external source. Sometimes you even get the Schema's of the source providers then you can deserialize even easier, XML works like that with XSD's.

其他回答

DTO的一个主要用例是从web服务返回数据。在这种情况下,POCO和DTO是等价的。POCO中的任何行为在从web服务返回时都将被删除,因此它是否具有行为并不重要。

甚至不要称它们为dto。他们被称为模型....句号。模特从来没有行为。我不知道是谁想出了这个愚蠢的术语DTO,但它一定是一个。net的东西,这是我所能想到的。想想MVC中的视图模型,同样的东西,模型是用来在层与层之间传递状态的服务器端或线上传输,它们都是模型。属性与数据。这些是你传递的模型。模特,模特。就是这样。

我希望DTO这个愚蠢的术语能从我们的词汇表中消失。

下面是一般规则:DTO=邪恶和过度工程软件的指示器。少= =好。“企业”模式已经摧毁了Java EE世界中许多人的大脑。请不要在。net领域重复这个错误。

DTO objects are used to deserialize data into objects from different sources. Those objects are NOT your Model (POCO) objects. You need to transform those objects into your Model (POCO) objects. The transformation is mostly a copy operation. You can fill those POCO objects directly from the source if its an internal source, but its not adviceable if its an external source. External sources have API's with descriptions of the Schema they use. Its much easier then to load the request data in an DTO and after that transform those in your POCO's. Yes its an extra step, but with a reason. The rule is to load the data from your source in an object. It can be JSON, XML whatever. When loaded then transform that data in what you need in your model. So most of times the DTO is an object image of the external source. Sometimes you even get the Schema's of the source providers then you can deserialize even easier, XML works like that with XSD's.

POCO只是一个不依赖于外部框架的对象。这是显而易见的。

POCO是否有行为无关紧要。

DTO可以是POCO,也可以是域对象(通常行为丰富)。

通常dto更可能依赖于外部框架(例如。属性)用于序列化的目的,因为它们通常在系统的边界处退出。

在典型的Onion风格架构中(通常在广泛的DDD方法中使用),领域层被放置在中心,因此它的对象在这一点上不应该有该层之外的依赖关系。