mostlylucid

STATIC ARCHIVE of mostlylucid.co.uk of old
posts - 916, comments - 758, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

AutoMapper question…any ideas…

In the little app I'm working on I'm using MongoDB...which is great, however MongoDB only supports storing Document objects. Which are kinda like Dictonaries (Document["blah"] = "bar";). I want to use AutoMapper to map these loosely types Key-Value Pairs to my objects and I'm basing it on some code I found drifting around:

This works:

 

 public static IMappingExpression<Document, TDestination> ConvertFromDocument<TDestination>(this IMappingExpression<Document, TDestination> exp, Func<string, string> propertyNameMapper)
        {
            foreach (PropertyInfo pi in typeof(TDestination).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                if (!pi.CanWrite ||                pi.GetCustomAttributes(typeof(TDestination), false).Length == 0)
                {
                    continue;
                }
                string propertyName = pi.Name;
                propertyName = propertyNameMapper(propertyName);
                exp.ForMember(propertyName, cfg => cfg.MapFrom(r=> r[propertyName]));
            }
            return exp;
        }

This maps my document to an object...cool...however, I'm having real problems figuring out the reverse (my lambda / knowledge of AutoMapper are letting me down):

This is NO way works...

  public static IMappingExpression<TSource, Document> ConvertToDocument<TSource>(this IMappingExpression<TSource, Document> exp, Func<string, string> propertyNameMapper)
        {  
            if (typeof(TSource) is IBaseObject)
            {
               foreach(PropertyInfo pi in typeof(TSource).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
               {
                     
                   string propertyName =pi.Name;
                   propertyName = propertyNameMapper(propertyName);
                         exp.ForMember(r=>r[propertyName], cfg=>cfg.MapFrom(p=>pi.GetValue(p));
               }
            }
            else
            {
                throw new Exception("What type are you trying to give me???");
            }
            return exp;
        }

 

Help my Jimmy Bogard, you're my only hope :)

Print | posted on Monday, February 15, 2010 4:13 PM |

Feedback

Gravatar

# re: AutoMapper question...any ideas...

So this one has actually come up a few times in feature requests. For now, I'd say create a custom IObjectMapper that would do this for you. You'd configure it so that any time a dictionary type is the source type and a non-dictionary type on the left, this mapper would execute. It's something I'm looking at after I get the SL 3.0 version working.

If you actually get this working, let me know so I can fold it in for everyone else :)

2/17/2010 4:37 PM | Jimmy Bogard

Comments have been closed on this topic.

Powered by: