POCO =普通旧CLR(或更好的:类)对象
DTO =数据传输对象
在这篇文章中有一个区别,但坦率地说,我读过的大多数博客都是用DTO的定义方式描述POCO的:DTO是简单的数据容器,用于在应用程序的层之间移动数据。
POCO和DTO是同一个东西吗?
POCO =普通旧CLR(或更好的:类)对象
DTO =数据传输对象
在这篇文章中有一个区别,但坦率地说,我读过的大多数博客都是用DTO的定义方式描述POCO的:DTO是简单的数据容器,用于在应用程序的层之间移动数据。
POCO和DTO是同一个东西吗?
当前回答
下面是一般规则:DTO=邪恶和过度工程软件的指示器。少= =好。“企业”模式已经摧毁了Java EE世界中许多人的大脑。请不要在。net领域重复这个错误。
其他回答
POCO遵循面向对象的规则。它应该(但不必)具有状态和行为。POCO来自于POJO,由Martin Fowler创造。他使用术语POJO作为一种方式,使其更性感地拒绝框架繁重的EJB实现。在. net中,POCO应该在相同的上下文中使用。不要让框架支配你的对象设计。
DTO的唯一目的是传输状态,不应该有任何行为。请参阅Martin Fowler对DTO的解释,以获得使用此模式的示例。
区别在于:POCO描述了一种编程方法(老式的面向对象编程),而DTO是一种使用对象来“传输数据”的模式。
虽然您可以像对待dto一样对待poco,但是如果您这样做,就会有创建贫血域模型的风险。此外,还存在结构上的不匹配,因为dto应该被设计为传输数据,而不是代表业务领域的真实结构。这样做的结果是dto往往比您的实际域更平坦。
在任何合理复杂的域中,最好创建单独的域poco并将它们转换为dto。DDD(领域驱动设计)定义了反腐败层(这里有另一个链接,但最好的方法是购买这本书),这是一个很好的结构,使隔离清晰。
下面是一般规则:DTO=邪恶和过度工程软件的指示器。少= =好。“企业”模式已经摧毁了Java EE世界中许多人的大脑。请不要在。net领域重复这个错误。
我认为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说这种方法是一条艰难的道路,只有在域层和用户界面之间存在严重不匹配的情况下才应该采用。
DTO的一个主要用例是从web服务返回数据。在这种情况下,POCO和DTO是等价的。POCO中的任何行为在从web服务返回时都将被删除,因此它是否具有行为并不重要。
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.