Example of how to use AutoMapper v5 and Simple Injector?

969 views
Skip to first unread message

rlig...@gmail.com

unread,
Jul 18, 2016, 3:15:26 PM7/18/16
to AutoMapper-users
Does anyone have a working example of this?

Thanks!

Jimmy Bogard

unread,
Jul 18, 2016, 3:57:36 PM7/18/16
to automapper-users
How would you like to use it with Simple Injector, what lifecycles do you want for the configuration and mapper instances? Here's one way:


On Mon, Jul 18, 2016 at 2:15 PM, <rlig...@gmail.com> wrote:
Does anyone have a working example of this?

Thanks!

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Raine Lightner

unread,
Jul 18, 2016, 3:59:59 PM7/18/16
to AutoMapper-users
This is how I implemented it earlier, and any call to Mapper.Map would fail saying it didn't recognize the mapping.

Do I need to passing in the IMapping via my constructor now?

-Raine

Jimmy Bogard

unread,
Jul 18, 2016, 4:18:27 PM7/18/16
to automapper-users
Oh if you're doing it statically, why would you use SimpleInjector at all? Just call Mapper.Initialize and Mapper.Map.

Raine Lightner

unread,
Jul 18, 2016, 4:31:57 PM7/18/16
to AutoMapper-users
I guess that's what I'm trying to figure out. Should I stick with Static or integrate all the way with SimpleInjector :)

Jimmy Bogard

unread,
Jul 18, 2016, 4:43:54 PM7/18/16
to automapper-users
So concretely, the reason why you'd want to integrate with SimpleInjector is that you have custom resolvers or type converters that need DI. You're never going to mock IMapper in a unit test, so there's no reason to introduce that dependency just for testing. You'd have it because an IMapper would get created with a callback to instantiate resolvers etc. via the container.

Raine Lightner

unread,
Jul 20, 2016, 9:55:12 AM7/20/16
to AutoMapper-users
Jimmy, thanks again for the feedback. Totally right about not mocking IMapper, so I've left it out of my DI world :)

-Raine

Jimmy Bogard

unread,
Jul 20, 2016, 10:05:42 AM7/20/16
to automapp...@googlegroups.com
I'm writing a blog today about this since it comes up often. Hopefully it answers your questions!
Reply all
Reply to author
Forward
0 new messages