How to register a custom ObjectMapper with AutoMapper

386 views
Skip to first unread message

Dinesh

unread,
Mar 15, 2010, 2:22:01 PM3/15/10
to AutoMapper-users
I am planning to create my Own Custom Object Mapper for a type using
AutoMapper's IObjectMapper interface, but i don't see any place where
we can register the implemented mapper with AutoMapper. Could anyone
tell me how to register it.

Regards
Dinesh

CVertex

unread,
Mar 16, 2010, 4:32:58 AM3/16/10
to AutoMapper-users
Howdy Dinesh,

From what I understand, in order to use any custom mappers, you must
forgo Mapper static class usage and bootstrap the MappingEngine
yourself.

Jimmy has a good post about this

http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/05/11/automapper-and-ioc.aspx

hope that helps,
CV

Jimmy Bogard

unread,
Mar 16, 2010, 8:25:23 AM3/16/10
to automapp...@googlegroups.com
Another way to go here is to replace the static registry function in the MapperRegistry class.  Here's the current version:

        public static Func<IEnumerable<IObjectMapper>> AllMappers = () => new IObjectMapper[]
        {
#if !SILVERLIGHT
            new DataReaderMapper(),
#endif
            new TypeMapMapper(TypeMapObjectMapperRegistry.AllMappers()),
            new StringMapper(),
            new FlagsEnumMapper(),
            new EnumMapper(),
            new ArrayMapper(),
new EnumerableToDictionaryMapper(),
            new DictionaryMapper(),
#if !SILVERLIGHT
            new ListSourceMapper(),
#endif
            new EnumerableMapper(),
            new AssignableMapper(),
            new TypeConverterMapper(),
            new NullableMapper()
        };

You'd basically copy that one, and do something like:

MapperRegistry.AllMappers = () => new IObjectMapper[] {
  // Insert your custom mapper somewhere in this list
};

Just do that before you do any Mapper.CreateMap or Mapper.Initialize business.  Mappers are evaluated in order.  This is one of the areas I'm looking at improving for the next version, to make adding custom mappers more obvious.

HTH,

Jimmy

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To post to this group, send email to automapp...@googlegroups.com.
To unsubscribe from this group, send email to automapper-use...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/automapper-users?hl=en.


Dinesh

unread,
Mar 16, 2010, 10:58:21 AM3/16/10
to AutoMapper-users
Thanks Jimmy, This would mean that i need to first fetch the current
list of mappers and then replace the function by adding my own one, so
that i can maintain all.

I was initially expecting it to be part of the Initialize method
something like
Mapper.Initialize (cfg => cfg.AddMapper<Type>(customMapper))

The CustomMapper that i am trying to implement is for
NameValueCollection, is that some thing on your map. I see some
discussions, but nothing suggesting that it will be implemented.

Regards
Dinesh

> >http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/05/11/autom...


>
> > hope that helps,
> > CV
>
> > On Mar 16, 5:22 am, Dinesh <dinesh...@gmail.com> wrote:
> > > I am planning to create my Own Custom Object Mapper for a type using
> > > AutoMapper's IObjectMapper interface, but i don't see any place where
> > > we can register the implemented mapper with AutoMapper. Could anyone
> > > tell me how to register it.
>
> > > Regards
> > > Dinesh
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "AutoMapper-users" group.
> > To post to this group, send email to automapp...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > automapper-use...@googlegroups.com<automapper-users%2Bunsubscrib e...@googlegroups.com>

Jimmy Bogard

unread,
Mar 16, 2010, 12:21:25 PM3/16/10
to automapp...@googlegroups.com
Hi Dinesh,

Yes, that kind of API is something I'm looking at, or similar.  If it's not Type based, then it would be Func<Type> based, so that you could look at just about anything on it.  The real important thing is order, so I'm brainstorming ideas on how to do that in a sane manner, especially if you have a special mapper that you want to insert before one of the generalized ones.

Can you elaborate some more on the namevaluecollection map?  This might be something I'd just incorporate anyway.

Thanks,

Jimmy

To unsubscribe from this group, send email to automapper-use...@googlegroups.com.

Dinesh

unread,
Mar 17, 2010, 10:58:08 AM3/17/10
to AutoMapper-users
Hi Jimmy,
For an upcoming project (due to start next week) i wanted to follow
the MVP Pattern and as part of that i wanted to restrict direct access
by my team to the common NameValueCollections like
Request.QueryString, Request.Form and AppSettings. I wanted to access
all of them through Predefined DataObjects so that i would be able to
inject them for tests as required.
I needed some kind of mapping so that i can map the data in
NameValueCollection as properties in my DataObject. I recently came
across the NameValueDeserializer which is part of MvcContrib which
does that work. While i could just use that directly for my purpose i
thought this was something that fits concept wise in AutoMapper and
can be implemented as a custom mapper, also hopefully contribute it
back.

NameValueDeserializer is available at
http://mvccontrib.codeplex.com/sourcecontrol/network/Show?projectName=MVCContrib&changeSetId=92cf452ab15e#src%2fMVCContrib%2fMetaData%2fNameValueDeserializer.cs

Regards
Dinesh

> > > > automapper-use...@googlegroups.com<automapper-users%2Bunsubscrib e...@googlegroups.com><automapper-users%2Bunsubscrib

Jimmy Bogard

unread,
Mar 19, 2010, 12:27:33 PM3/19/10
to automapp...@googlegroups.com
Sure, looks interesting.  You can send me a patch (or a github pull request, that's preferred).

Thanks!

Jimmy

To unsubscribe from this group, send email to automapper-use...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages