Category Archives: Patrón

Data Transfer Objects in Domain-Driven Design Patterns

Data Transfer Object. A simple pattern for exchanging information between the Presentation Layer and the Service Layer.

The basics are explained in lots of articles all over the web, but there are some details that are not so obvious. I implemented DTOs following my instincts, but I always had the feeling I was asuming things and not doing it well.

That’s whay I keep on asking about the insights of patterns like this.

I found a very interesting article about DTOs by Bozhidar. I recommend you to read it because it gives several hints and advices on how to use this pattern sucessfully.

Bozho’s tech blog – On DTOs

The last part I want to notice is the question I posted. As you see, Bozher was really kind and answered really quick.

Thanks!

Translator Pattern. Know Your Patterns :)

 

image

 

This pattern is really simple. It provides a method to convert from a representation to another. The same concept can be represented in many ways. Sometimes you have to “import” and “export” representations, so you have to provide a proper encapsulation for the logic that converts from one object to another.

Why would you need to do that? In my personal experience I have used it to convert instances of “legacy” clases that are not too well designed. It happens that a lot of logic already exists in the solution that work with those clases, so redefining them would be really expensive. The brand new features use better an more robust clases. For newer parts, I use a translator to perform a one time time conversion. When a legacy part of the application needs a “new” instance, I request a conversion to the old class instance.

It can be seen as a pattern similar to Adapter or Builder. But Adapter translates interfaces and provide a “wrapper” that interacts between the outer world and the wrapped classes along all its lifecycle. Builder creates complex objects. It’s a similar pattern, but I don’t feel it’s the same as the builder doesn’t translate, it builds up an instance.

What do you think about it? Do you see a Builder here? Smile

Differences between Abstract Factory / Builder / Adapter

When it comes to patterns, it’s important to know the differences. Sometimes you have to name a class and you tend to call it “a X-builder”, or a “X-Generator”. But… are you sure you gave it the correct name?

It’s important to name things properly in our projects. From variables, to methods and classes. It REALLY matters. Even more when you are part of a team. When we are implementing a pattern we should give every entity the right now to avoid ambiguity.

In this particular case, those patterns may seem to do similar things, but they don’t. So, how to identify each one?

image

I hope somebody is kind enough to point out the differences between them. Come on, participate! Comments are welcome.

Duck Typing FTW

Para que no se te olvide, golferas, si quieres programar como Dios manda deberías hacerlo siempre intentando programar “contra” interfaces, aunque no es una expresión que me guste mucho eso de “contra”, pero así todo el mundo lo entiende.

Para facilitarnos la tarea existe una técnica llamada Duck Typing.

“Si anda como un pato, nada como un pato y vuela como un pato, entonces es un pato”.

En pocas palabras Duck Typing es conseguir tratar como un objeto fuese algo que realmente no lo es, aunque cumple con sus requisitos. Esto se traduce a tener tener un tipo que no implementa cierta interfaz, pero lo tratamos como si la implementase.

El gran Juan María Hernández (@gulnor) publica en su blog publica un artículo especialmente interesante donde explica muy bien qué es y cómo funciona, además de exponer una pequeña implementación de andar por casa ayudarnos a entender cómo funciona por dentro.

¿Cómo lo consigue? Con reflexión, invocando atributos y métodos como si realmente estuviésemos trabajando con el tipo que queremos.

Sin duda, un concepto revolucionario con lo que podemos crear código más abstracto  y menos acoplado, que es de lo que se trata.

Por cierto, si no quieres devanarte la sesera para hacerte tu propia implementación de esta , échale un vistazo al DynamicProxy o al aparentemente bueno ImpromptuInterface (http://code.google.com/p/impromptu-interface/).

Venga, a probarlo ya, ¡jodebles!