Translator Pattern

丶灬走出姿态 提交于 2019-12-03 15:36:01

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.

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).

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!