views:

282

answers:

4

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?

A: 

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

Jess
A: 

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.

Richard Quinn
His question was about the translator pattern, not about how to accomplish the task. Also the OP does not say he is using a specific version of the framework, Linq may not be appropriate in his case.
GrayWizardx
Very true. Nevertheless I provided a code example of how to achieve the task elegantly for anyone chancing on this page with a similar task, and I provided a URL to the (near) original source of the TranslatorPattern for self study.
Richard Quinn
@GrayWizardx, right on. In my situation I was dealing with an API. I'm not sure what Richard's query example is about, as the issue had nothing to do with a query.
Larry Watanabe
+2  A: 

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.

GrayWizardx
Thanks GrayWizardx!
Larry Watanabe
+1  A: 

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

Igor Zevaka
Thanks Igor, this was a useful link - both about the Entity Translator and about the existence of Microsoft Patterns in general.
Larry Watanabe