views:

23

answers:

0

In my limited experience, it seems that a common idiom is to pass objects from one layer to another using a "mapper". An ORM is one example, to go map from the Domain to the database and back. Automapper is another, which facilitates mapping from a rich domain object, into an object consumable by another layer (ViewModel, WebService. So I know that most ORMs utilize reflection in order to populate and retrieve values for private fields, thus freeing you to encapsulate your object however you want (you don't have to expose every single member). I always looked at this blatant violation of encapsulation on the part of the mapper as acceptable, since it is supposed to be a transparent tool. If I was writing my own Mapping layer by hand, which would not involve automated tools (let's say I need a layer which maps my ViewModel back to my Domain model, which can't be done automatically), is it ok to use reflection? All the examples I've seen of this pattern are in .NET and the author always blindly creates public properties for every member. But I'm trying to do without that, and in the language I'm using (PHP), there is no support for automatic properties, leaving me either to use reflection, or dozens of ugly "Get and Set" methods.