翻译模式

发布于 2024-08-15 17:21:23 字数 267 浏览 4 评论 0 原文

在之前的工作中,我的经理建议使用转换器模式将数据从 DataTable 转换为对象。基本上,Translator 类只有静态(即类)方法,因此它是函数调用的聚合。我最初的方法是为每个对象实现构造函数,该对象可以将 DataTable 行作为参数并创建与数据相对应的实例。

他说Translator类是微软建议的,并且提供了更好的代码模块化。我可以看到这一点,但同时这似乎是一种非常非 OO 的方法(尽管访问者模式具有类似的特征)。

你们中有人使用过这种模式吗?您对此有何看法?优点和缺点?

In a previous job, my manager suggested use of a Translator pattern for converting data from a DataTable to objects. Basically, the Translator class had only static (i.e. class) methods so it was an aggregation of function calls. My initial approach was to implement constructors for each object that could take a DataTable row as an argument and create an instance that corresponded to the data.

He said that the Translator class had been suggested by Microsoft, and provided better modularity of code. I can see this point, but at the same time it seems like a very non-OO approach (although the Visitor pattern has similar characteristics).

Have any of you used this pattern, and what do you think of it? pros and cons?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

却一份温柔 2024-08-22 17:21:23

C2.Com 看来,翻译器模式是访问者模式的非 OOP 实现。它在文章末尾指出了一些缺点,包括在 OOP 语义中很难表达(但不是代码)的事实,换句话说,它可以很好地工作,但如果您是这样的话,可能没有多大意义。对其余代码使用纯 OOP。

From C2.Com it appears that the Translator pattern is a non-OOP implementation of the visitor pattern. It notes and the end of the article a few of the drawbacks, including the fact that in OOP semantics it is difficult to express (but not code), in other words it will work fine but may not make a lot of sense if you are using pure OOP for the rest of your code.

百善笑为先 2024-08-22 17:21:23

我认为您正在谈论实体翻译器。我认为这种场景下的翻译器自然是静态方法。它生活在哪里是一个美学问题。它也应该很容易进行单元测试,因为它应该只依赖于它之间转换的两个数据结构。听起来他们的“数据契约”的另一个名字是 DTO(数据传输对象)。

I think you are talking about Entity Translator. I think that the translator in this scenario is naturally a static method. Where it lives is a matter of aesthetics. It should also be quite easily unit tested as it should only have dependencies on two data structures that it translates between. Kind of sounds like another name for their "data contract" is DTO (Data Transfer Object).

客…行舟 2024-08-22 17:21:23

如果您可以在没有任何外部依赖项的情况下执行映射,那么除了静态方法之外,使用任何其他方法都没有任何用处。

If you can perform the mapping without any external dependencies, then there's really no use in utilizing anything other than a static method.

回首观望 2024-08-22 17:21:23

也许我错过了一些东西,但为什么不直接使用 linq 呢?

    IEnumerable<Customer> customerQuery =
    from cust in customers
    where cust.City == "London"
    select cust;

foreach (Customer customer in customerQuery)
{
    Console.WriteLine(customer.LastName + ", " + customer.FirstName);
}

无论如何,TranslatorPattern 是将数据结构从一种表示形式更改为另一种等效结构。这里 http://c2.com/cgi/wiki?TranslatorPattern 是有关该内容的更深入信息。

Maybe I'm missing something, but why not just use linq?

    IEnumerable<Customer> customerQuery =
    from cust in customers
    where cust.City == "London"
    select cust;

foreach (Customer customer in customerQuery)
{
    Console.WriteLine(customer.LastName + ", " + customer.FirstName);
}

Anyway, the TranslatorPattern is about changing the data structure from one representation to another equivalent structure. Here http://c2.com/cgi/wiki?TranslatorPattern is the deeper info on that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文