﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Udi Dahan commented on Object / Object mapping</title><description>I'm seeing more value specifically in message/relational mapping than a more generic object/object mapping. A message being an implementation of the DTO pattern.
  
  
I posted something on this topic a while ago [http://udidahan.weblogs.us/2007/04/15/lazy-loading-and-how-messaging-fixes-everything-again/]
  
  
The main idea is to decrease memory utilization (consider what happens when a customer has a million orders) or any of those "enterprise" issues. The last thing you want is to first load a bunch of domain objects in to memory, and then duplicate that same amount of memory in a new set of structures.
  
  
I've found this to be particularly useful for GetXXX methods on the service layer - where the Domain Model provides little value anyway.
  
  
The best part is that this is already supported by all the O/RM tools.
</description><link>http://ayende.com/3009/object-object-mapping#comment24</link><guid>http://ayende.com/3009/object-object-mapping#comment24</guid><pubDate>Mon, 10 Dec 2007 12:32:03 GMT</pubDate></item><item><title>Zdeslav Vojkovic commented on Object / Object mapping</title><description>If you only have 'stupid' DTOs without much behavior, I would say that it's OK. However, if you are doing mapping to a type which implements some non-trivial behavior, I would try to avoid combining the transformation logic into the type itself, to keep the responsibilities separated.
  
  
Additional downside is that mappings defined by attributes are probably not type safe (i don't know how it is implemented in your case, but i presume that string expressions are used to define mapping). The better solution would be to use delegates but this can look quite ugly when used as attribute parameter, especially in more complicated mappings, which defeats the purpose of such mapping, in a way.
</description><link>http://ayende.com/3009/object-object-mapping#comment23</link><guid>http://ayende.com/3009/object-object-mapping#comment23</guid><pubDate>Fri, 07 Dec 2007 18:58:55 GMT</pubDate></item><item><title>Pedro Teixeira commented on Object / Object mapping</title><description>I'm currently using metadata attributes on the DTO classes to allow a generic engine to map/transform (from entities and to entities). Is this considered bad practice?
  
  
[]'s
</description><link>http://ayende.com/3009/object-object-mapping#comment22</link><guid>http://ayende.com/3009/object-object-mapping#comment22</guid><pubDate>Thu, 06 Dec 2007 16:28:32 GMT</pubDate></item><item><title>Jon Skeet commented on Object / Object mapping</title><description>Ayende,
  
  
You appear to be duplicating the work of MulticastDelegate. I'd just create a single delegate containing multiple actions, and then call that once. It will run through all the actions appropriately.
  
  
The one downside is that to initialize the delegate instance as an expression, you need to cast the first one to Transformer before combining it with the second:
  
  
Transformer t = (Transformer) ((y, z) =&gt; z.ID = y.Id) + 
  
    ((x,z) =&gt; z.CustomerName = x.Name);
  
  
Jon
</description><link>http://ayende.com/3009/object-object-mapping#comment21</link><guid>http://ayende.com/3009/object-object-mapping#comment21</guid><pubDate>Thu, 06 Dec 2007 09:59:21 GMT</pubDate></item><item><title>Andrey Shchekin commented on Object / Object mapping</title><description>&gt; Like I said, this approach lets you add stuff at runtime.
  
I think you can combine the approaches -- I will not try to build it, but basically it would be something like:
  
List&lt;Transformer&gt; transforms = SplitTransforms(order =&gt; new OrderDTO {
  
   ID = order.Id,
  
   CustomerName = order.Name
  
});
  
and
  
List&lt;Transformer&gt; SplitTransforms(Expresssion&lt;Func&lt;Order, OrderDto&gt;&gt; transformExpression) { ... }
</description><link>http://ayende.com/3009/object-object-mapping#comment20</link><guid>http://ayende.com/3009/object-object-mapping#comment20</guid><pubDate>Thu, 06 Dec 2007 06:55:50 GMT</pubDate></item><item><title>bennyb commented on Object / Object mapping</title><description>That is why I use Dictionaries as data transfer objects. You don't have to change anything when properties change. Furthermore, I transfer object for all domain objects.
  
  
public class TransferObject
  
{
  
    private Dictionary&lt;string, object&gt; _fields = new Dictionary&lt;string, object&gt;();
  
  
    public Dictionary&lt;string, object&gt; Fields
  
    {    
  
        get { return _fields; }
  
        set { _fields = value; }
  
    }
  
  
    public object this[string key]
  
    {
  
        get{return _fields[key];}
  
        set { _fields[key] = value; }
  
    }
  
}
  
  
Them they can be easily transformed 
  
  
public T Transform&lt;T&gt;(TransferObject from) where T : new()
  
{
  
	T to = new T();
  
        //loop thru properties and set their
  
	return to;
  
}
  
  
</description><link>http://ayende.com/3009/object-object-mapping#comment19</link><guid>http://ayende.com/3009/object-object-mapping#comment19</guid><pubDate>Wed, 05 Dec 2007 20:20:53 GMT</pubDate></item><item><title>John St. Clair commented on Object / Object mapping</title><description>And i would say this is one of those demo cases for extension methods:
  
  
    public static class DTOTransforms
  
    {
  
        public static OrderDTO ToDTO(this Order o)
  
        {
  
            return new OrderToOrderDTO().Transform(o);
  
        }
  
    }
  
  
so you can say:
  
  
Order order = new Order() { Id = 5, Name = "John St. Clair" };
  
OrderDTO dto = order.ToDTO();
  
</description><link>http://ayende.com/3009/object-object-mapping#comment18</link><guid>http://ayende.com/3009/object-object-mapping#comment18</guid><pubDate>Wed, 05 Dec 2007 19:42:00 GMT</pubDate></item><item><title>Francois Tanguay commented on Object / Object mapping</title><description>At least, I would generalize it a bit:
  
  
public delegate void Transformer&lt;TFrom, TTo&gt;(TFrom from, TTo to);
  
  
List&lt;Transformer&lt;Order, OrderDTO&gt;&gt; transforms = new List&lt;Transformer&lt;Order, OrderDTO&gt;&gt;()
  
{
  
  (from, to) =&gt; to.ID = from.Id, 
  
  (from, to) =&gt; to.CustomerName = from.Name
  
};
  
  
public TTo Transform&lt;TFrom, TTo&gt;(TFrom from)
  
  where TTo : new()
  
{
  
  TTo to = new TTo();
  
  transforms.ForEach((item) =&gt; item(from, to));
  
  return to;
  
}
  
</description><link>http://ayende.com/3009/object-object-mapping#comment17</link><guid>http://ayende.com/3009/object-object-mapping#comment17</guid><pubDate>Wed, 05 Dec 2007 13:35:12 GMT</pubDate></item><item><title>Emanuele DelBono commented on Object / Object mapping</title><description>It's a side of LINQ that only few people talk about.
  
  
I talked about this on my blog some weeks ago.
  
Here is the link: http://blogema.wordpress.com/2007/11/13/dto-and-linq/
  
  
bye
</description><link>http://ayende.com/3009/object-object-mapping#comment16</link><guid>http://ayende.com/3009/object-object-mapping#comment16</guid><pubDate>Wed, 05 Dec 2007 10:00:10 GMT</pubDate></item><item><title>Zdeslav Vojkovic commented on Object / Object mapping</title><description>I usually need O/O mapping on the client side, e.g. in the presentation model. However, besides simple 1:1 mapping, i often need to flatten the objects, do some calculations, convert IList/ICollection to array and vice versa, and so on. 
  
I solved this by creating a small mapper library (http://otis-lib.googlecode.com) which supports mapping via xml mapping definitions or attributes on type members.
  
I am still not convinced that it was worth the effort for a relatively simple problem, but thruth be told, it was fun and a nice chance for me to start playing with CodeDOM and some other stuff.
</description><link>http://ayende.com/3009/object-object-mapping#comment15</link><guid>http://ayende.com/3009/object-object-mapping#comment15</guid><pubDate>Wed, 05 Dec 2007 09:53:23 GMT</pubDate></item><item><title>Ayende Rahien commented on Object / Object mapping</title><description>Andrey,
  
Like I said, this approach lets you add stuff at runtime.
  
However, the truth is that I never considered the initializer.
</description><link>http://ayende.com/3009/object-object-mapping#comment14</link><guid>http://ayende.com/3009/object-object-mapping#comment14</guid><pubDate>Wed, 05 Dec 2007 08:49:43 GMT</pubDate></item><item><title>Andrey Shchekin commented on Object / Object mapping</title><description>I do not get why this is better then object initializers.
  
public OrderDTO Transform(Order k)
  
{
  
     return new OrderDTO {
  
          ID = k.Id,
  
          CustomerName = k.Name
  
     }
  
}
  
is way easier in my opinion.
</description><link>http://ayende.com/3009/object-object-mapping#comment13</link><guid>http://ayende.com/3009/object-object-mapping#comment13</guid><pubDate>Wed, 05 Dec 2007 07:45:12 GMT</pubDate></item><item><title>Ayende Rahien commented on Object / Object mapping</title><description>Jeff,
  
I never said it was a _good_ thing. Just that it is something that works.
  
You can make the arguments that now you can add stuff to the conversion dynamically, but that isn't the reason, I just wanted to see it done.
</description><link>http://ayende.com/3009/object-object-mapping#comment12</link><guid>http://ayende.com/3009/object-object-mapping#comment12</guid><pubDate>Wed, 05 Dec 2007 06:45:02 GMT</pubDate></item><item><title>Jeff Perrin commented on Object / Object mapping</title><description>Ok then, I'm stumped. Why on earth would I do this rather than:
  
  
public class OrderToDtoConverter{
  
  public OrderDto Convert(Order o){
  
    var dto = new OrderDto();
  
    dto.Id = o.Id;
  
    return dto;
  
  }
  
}
</description><link>http://ayende.com/3009/object-object-mapping#comment11</link><guid>http://ayende.com/3009/object-object-mapping#comment11</guid><pubDate>Wed, 05 Dec 2007 03:56:39 GMT</pubDate></item><item><title>Mats Helander commented on Object / Object mapping</title><description>@Ayende,
  
  
Well, you may still want things like lazy loading, inverse management, dirty tracking, original value tracking for optimistic concurrency, etc etc....basically all the stuff I refer to as Domain Model Management (DMM) - perhaps not in a straight DTO case, but in many other O/O scenarios (such as mapping a presentation model to your domain model). 
  
  
The actual transformation part, however, is obviously straightforward :-) But then I often argue that the Object/Relational transformation - mapping classes and properties to tables and columns - is also fairly straightforward and is often in fact a rather small part of what an O/R Mapper does (at least in cases where the mapper will also do a lot of DMM stuff such as lazy loading etc). 
  
  
So you do have a point - a _pure_ O/O Mapper would be (almost) that simple...but then, so would (almost) a _pure_ O/R Mapper...well a little more complicated perhaps, but not really that much. However as soon as you start adding DMM to the mix the complexity blows up. And there is just as much reason to support DMM features such as lazy loading and dirty tracking in an O/O Mapper as in an O/R Mapper. Both will, assumably, be dealing with at least one domain model.  
  
  
/Mats
</description><link>http://ayende.com/3009/object-object-mapping#comment10</link><guid>http://ayende.com/3009/object-object-mapping#comment10</guid><pubDate>Wed, 05 Dec 2007 00:57:49 GMT</pubDate></item><item><title>jdn commented on Object / Object mapping</title><description>You can use anonymous types outside a method.
  
  
http://blogs.msdn.com/alexj/archive/2007/11/22/t-castbyexample-t-object-o-t-example.aspx
  
  
</description><link>http://ayende.com/3009/object-object-mapping#comment9</link><guid>http://ayende.com/3009/object-object-mapping#comment9</guid><pubDate>Wed, 05 Dec 2007 00:44:58 GMT</pubDate></item><item><title>Ayende Rahien commented on Object / Object mapping</title><description>Sergio,
  
CreateFrom will work, but I may want to do things like:
  
OrderToOrderDTO, OrderListToOrderDTO, etc.
  
  
It is the continuation of a thread in the alt.net mailing list.
</description><link>http://ayende.com/3009/object-object-mapping#comment8</link><guid>http://ayende.com/3009/object-object-mapping#comment8</guid><pubDate>Tue, 04 Dec 2007 23:50:07 GMT</pubDate></item><item><title>Ayende Rahien commented on Object / Object mapping</title><description>Jeff,
  
I am aware of Map. But I think that this approach looks better.
</description><link>http://ayende.com/3009/object-object-mapping#comment7</link><guid>http://ayende.com/3009/object-object-mapping#comment7</guid><pubDate>Tue, 04 Dec 2007 23:48:31 GMT</pubDate></item><item><title>Sergio Pereira commented on Object / Object mapping</title><description>OK, I have to ask.
  
Why do you need this list of delegates? Wouldn't a simple CreateFrom method do the transformation more clearly? Maybe you're trying to achieve something other than what I thought.
  
</description><link>http://ayende.com/3009/object-object-mapping#comment6</link><guid>http://ayende.com/3009/object-object-mapping#comment6</guid><pubDate>Tue, 04 Dec 2007 23:47:48 GMT</pubDate></item><item><title>Jeff Perrin commented on Object / Object mapping</title><description>ConvertAll is what you want. There is already an established pattern for this (Map). http://diditwith.net/2007/06/21/AHigherCalling.aspx
</description><link>http://ayende.com/3009/object-object-mapping#comment5</link><guid>http://ayende.com/3009/object-object-mapping#comment5</guid><pubDate>Tue, 04 Dec 2007 23:42:32 GMT</pubDate></item><item><title>Ayende Rahien commented on Object / Object mapping</title><description>Will, I don't see this as painful.
  
Anonymous types are only useful within a method, not outside of it. Not really helpful in this regard.
</description><link>http://ayende.com/3009/object-object-mapping#comment4</link><guid>http://ayende.com/3009/object-object-mapping#comment4</guid><pubDate>Tue, 04 Dec 2007 22:55:29 GMT</pubDate></item><item><title>Will commented on Object / Object mapping</title><description>For one-way transfer, what about using anonymous types with Linq?
  
  
The whole DTO thing is a real pain in general.  Going from the Domain to DTOs and then serializing those to JavaScript DTOs and then trying to bring the data back again turned out to be a hell of a lot more pain than I expected.
  
  
I was half expecting you to have some whiz-bang solution to this when I read the title.  I suppose you are human after all...
</description><link>http://ayende.com/3009/object-object-mapping#comment3</link><guid>http://ayende.com/3009/object-object-mapping#comment3</guid><pubDate>Tue, 04 Dec 2007 22:52:37 GMT</pubDate></item><item><title>Ayende Rahien commented on Object / Object mapping</title><description>Mats,
  
I can guess. But please explain why.
</description><link>http://ayende.com/3009/object-object-mapping#comment2</link><guid>http://ayende.com/3009/object-object-mapping#comment2</guid><pubDate>Tue, 04 Dec 2007 22:43:04 GMT</pubDate></item><item><title>Mats Helander commented on Object / Object mapping</title><description>Hehe believe me, it can get plenty more complicated :-) 
  
  
NPersist supports O/O Mapping, it's pretty much as complex as O/R Mapping.
  
  
/Mats
</description><link>http://ayende.com/3009/object-object-mapping#comment1</link><guid>http://ayende.com/3009/object-object-mapping#comment1</guid><pubDate>Tue, 04 Dec 2007 22:41:32 GMT</pubDate></item></channel></rss>