普通旧 CLR 对象与数据传输对象 [英] Plain Old CLR Object vs Data Transfer Object

查看:37
本文介绍了普通旧 CLR 对象与数据传输对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

POCO = Plain Old CLR (or better: Class) Object

DTO = 数据传输对象

DTO = Data Transfer Object

在此帖子中 有区别,但坦率地说,我读过的大多数博客都以 DTO 的定义方式描述了 POCO:DTO 是用于在应用程序层之间移动数据的简单数据容器.

In this post there is a difference, but frankly most of the blogs I read describe POCO in the way DTO is defined: DTOs are simple data containers used for moving data between the layers of an application.

POCO 和 DTO 是一回事吗?

Are POCO and DTO the same thing?

推荐答案

POCO 遵循 OOP 的规则.它应该(但不是必须)有状态行为.POCO 来自 POJO,由 Martin Fowler 创造 [这里的轶事].他使用术语 POJO 作为一种方式,使拒绝框架繁重的 EJB 实现变得更加性感.POCO 应该在 .Net 中的相同上下文中使用.不要让框架决定您对象的设计.

A POCO follows the rules of OOP. It should (but doesn't have to) have state and behavior. POCO comes from POJO, coined by Martin Fowler [anecdote here]. He used the term POJO as a way to make it more sexy to reject the framework heavy EJB implementations. POCO should be used in the same context in .Net. Don't let frameworks dictate your object's design.

DTO 的唯一目的是转移状态,不应有任何行为.请参阅 Martin Fowler 的 DTO 的说明,了解使用此模式的示例.

A DTO's only purpose is to transfer state, and should have no behavior. See Martin Fowler's explanation of a DTO for an example of the use of this pattern.

区别如下:POCO 描述了一种编程方法(优秀的老式面向对象编程),其中DTO 是一种模式,用于传输数据"使用对象.

Here's the difference: POCO describes an approach to programming (good old fashioned object oriented programming), where DTO is a pattern that is used to "transfer data" using objects.

虽然您可以像对待 DTO 一样对待 POCO,但您可能会面临创建贫血域模型的风险

While you can treat POCOs like DTOs, you run the risk of creating an anemic domain model if you do so. Additionally, there's a mismatch in structure, since DTOs should be designed to transfer data, not to represent the true structure of the business domain. The result of this is that DTOs tend to be more flat than your actual domain.

在具有任何合理复杂性的域中,您几乎总是最好创建单独的域 POCO 并将它们转换为 DTO.DDD(域驱动设计)定义了 反腐败层(另一个链接此处,但最好的做法是买书),这是一个很好的结构,使隔离清晰.

In a domain of any reasonable complexity, you're almost always better off creating separate domain POCOs and translating them to DTOs. DDD (domain driven design) defines the anti-corruption layer (another link here, but best thing to do is buy the book), which is a good structure that makes the segregation clear.

这篇关于普通旧 CLR 对象与数据传输对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆