Translator Template

In a previous assignment, my manager suggested using the Translator template to convert data from a DataTable to objects. In principle, 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 can take a DataTable string as an argument and create an instance that matches the data.

He said that the Translator class was proposed by Microsoft and provided better code modularity. I see this point, but at the same time it seems to be a very non-OO approach (although the Visitor template has similar characteristics).

Have any of you used this template and what do you think of this? advantages and disadvantages?

+4
source share
4 answers

From C2.Com, it seems that the translator pattern is a non-OOP implementation of the visitor pattern. He notes at the end of the article some of the shortcomings, including the fact that in the semantics of OOP it is difficult to express (but not code), in other words, it will work fine, but may not make much sense if you are using pure OOP for the rest parts of your code.

+3
source

I think you are saying Entity Translator . I think that the translator in this scenario is naturally a static method. Where he lives is a matter of aesthetics. It should also be easily tested in a module, since it should only have dependencies on the two data structures that it translates. The kind of sounds similar to another name for their β€œdata contract” is the DTO (Data Transfer Object).

+4
source

If you can match without any external dependencies, then you really don't need to use anything other than a static method.

0
source

Maybe something is missing for me, 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); } 

In any case, TranslatorPattern is a change in the data structure from one view to another equivalent structure. Here http://c2.com/cgi/wiki?TranslatorPattern is more in-depth information about this.

-one
source