数据传输对象模式?
我遇到一个问题,我需要生成关于对象“客户”的基于 Word 的报告。
我通常通过将 Customer 传递给一个知道如何创建 Word 文档、插入书签等的类来完成此操作。
这样做的问题是,我发现自己将用于检索和格式化有关 Customer 对象的信息的逻辑放置在此类中。
我知道这很糟糕并且违反了单一责任原则。
我考虑创建另一个类,如“CustomerReportInfo”,它接受自定义并具有有关数据格式化和检索的所有逻辑,这样,Word 生成类很小,并且两者可以相互更改。
我只是想知道这个类应该叫什么? 我浏览了一系列模式,但实际上看不到任何符合描述的内容。 它听起来不像策略或代理...听起来像 DTO,但 DTO 通常是没有功能的愚蠢类,不是吗?
关于这类似于哪种模式有什么想法吗?
I've got a problem where I need to generate a Word based reports on object “Customer”.
I usually do this by passing Customer to a class which knows how to create a Word document, insert bookmarks etc.
The problem with this, is that I find myself placing the logic for retrieving and formatting information about the Customer object in this class.
I understand this is bad and violates the single responsibility principal.
I though about creating another class like “CustomerReportInfo” which accepts a Custom and has all the logic concerning the formatting and retrieving of data, that way, the Word generating class is small and the two can change irrespective of one another.
I'm just wondering what this class should be called? I've gone through a list of patterns and I can't really see anything which fits the description. It doesn't sound like a strategy, or proxy... It sounds like DTO, but DTOs are usually dumb classes with no functionality, aren’t they?
Any ideas on which pattern this resembles?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果特定于单词,我更喜欢 CustomerView 或 CustomerWordView。 您在这里接触到了 MVC 模式,在这种情况下,第三个概念是从数据库加载 Customer 的控制器类(最好要求其他人加载它)并构造视图对象。
I prefer CustomerView or CustomerWordView if it's word specific. You're touching on the MVC pattern here, in which case the third concept is the controller class that loads the Customer from the database (perferably asks someone else to load it) and constructs the view object.
我会这样称呼它,
我知道这听起来很虚幻,但它说明了它的作用,而无需发表评论或任何其他内容,
I would call it
I know it sounds poxy but it says what it does without having to comment or anything,
我不知道该模式的名称,但我喜欢 .NET 的应用程序架构:设计应用程序和服务。 我将调用报表服务
ReportService
并让它访问Customer
的 DAL。替代文本 http://i.msdn.microsoft .com/ms978348.f02aa01(zh-cn,MSDN.10).gif
I don't know the name of the pattern but I like Application Architecture for .NET: Designing Applications and Services. I'd call the report service
ReportService
and let it access the DAL forCustomer
.alt text http://i.msdn.microsoft.com/ms978348.f02aa01(en-us,MSDN.10).gif
你走在正确的轨道上,但还没有完全实现。 您希望每个类真正只承担一项职责,而不是创建一个负责从 Customer 对象检索数据并包含该数据的新类。 所以你创建了两个新类。
根据这篇文章,您似乎想要一个像这:
Word 报告生成器
是您当前用于生成报告的类。DTO
是一个尽可能简单的对象,同时仍然包含特定报告所需的所有数据。Data
是有关客户对象的信息。 然后有一个特殊的Assembler
类,其中包含“检索和格式化有关Customer
对象的信息的逻辑”并使用该信息填充 DTO。You're on the right track, but not quite there. Instead of creating one new class that is responsible for both retrieving data from the Customer object and containing that data, you want each class to truly have only one responsibility. So you create two new classes.
Based on this article, it seems like you would want an architecture like this:
The
Word Report Generator
is the class you're currently using to generate the report. TheDTO
is an object that is as simple as you can make it while still containing all of the data you will need for your specific report. TheData
is your information about the customer object. Then there is a specialAssembler
class that contains "the logic for retrieving and formatting information about theCustomer
object" and populating the DTO with that information.