Using Automapper with Unity Interception and overloaded DI constructors

426 views
Skip to first unread message

Ca Hy

unread,
Feb 2, 2015, 1:47:43 PM2/2/15
to automapp...@googlegroups.com
I have recently started using Automapper, in conjunction with Unity IOC, in a rewrite project, and have been very happy to find automapper. I've been able to fight my way through a number of issues with happy outcomes, but after much searching, experimenting, and reading, this one still has me stumped.  My problem is that I cannot get my automapped class methods wrapped with interception behaviors -- 

I'm hoping someone has some guidance for me.

I'm mapping objects using profiles. I need to be able to create Automap mapped objects using the concrete rather than proxies.  As such, I have setup, (and have working fine) some profiles that look like this: 

Mapper.CreateMap<MyObject, DomainModels.MyObject>() //<-- the concrete object
      .ConstructUsingServiceLocator();

Mapper.CreateMap<MyObject, IMyObject>()          //<-- for the interface
    .As<DomainModels.MyObject>();

Unity services the "ConstructUsingServiceLocator" in the first "CreateMap" allows us to inject some dependencies for the objects.  

The second "CreateMap" (also working fine) tells Automapper to construct the interface "IMyObject" as a "MyObject". This avoids issues with the proxies (no methods, missing the dependencies for the constructor, etc). 

So far so good -- this much works beautifully.

However, I have also setup Interceptors in Unity. My interceptors are setup as Interface Interceptors, and implement Unity's IInterceptionBehavior". They are really pretty generic -- and generally, my interceptors work fine.  But when I attempt to intercept an "AutoMapped class", calls are not wrapped with the interceptors.  I have verified Unity is adding the automapped classes as "needing interception" - they are setup exactly as my other classes that are working, so I believe the problem lies elsewhere.  


My suspicion is that the source of my problem is that my Interceptors are Interface Interceptors, which might happily wrap the interface "IMyObject" -- but because I am telling AutoMapper to create the mapped object as a "MyObject", interception doesn't see it and kick in.


What do I do to get these classes intercepted? It is not an option to remove the overloaded constructors or the DI for these classes. But I also really would like them wrapped. Help!


Thanks for taking the time to read - and help or suggestions would be appreciated.

Cathy



Jimmy Bogard

unread,
Feb 3, 2015, 9:45:52 AM2/3/15
to automapp...@googlegroups.com
Hmmmm...have you tried resolving these types straight from the container to see what comes back? And not through AutoMapper?

--
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.

Ca Hy

unread,
Feb 3, 2015, 10:33:44 AM2/3/15
to automapp...@googlegroups.com
Thank you very much for taking the time to reply -- 

I have - Resolved directly from the container, they resolve without issue, and do include the specified interceptor wrapping(s). I'm able to call the methods and step through the interceptors as well as the called methods. They work just as you'd expect.

Jimmy Bogard

unread,
Feb 3, 2015, 10:35:54 AM2/3/15
to automapp...@googlegroups.com
Well nuts. One potential workaround (for now) would be to resolve the model before passing into Mapper.Map, so you can do instead Mapper.Map(src, domain)

Ca Hy

unread,
Feb 3, 2015, 10:57:00 AM2/3/15
to automapp...@googlegroups.com

I'll give this a try later today and let you know what happens. It sounds like you're thinking that it should work: maybe I'll get a chance this weekend to debug into the automapper source a bit. It'd be interesting to see what I learn doing that. :)

Thank you again for the help -- 

Ca Hy

unread,
Feb 3, 2015, 11:34:50 AM2/3/15
to automapp...@googlegroups.com
Couldn't wait -- had to go ahead and try it. :)  It does work as you suggested; Thank you for the work around.

In order to get it to map without getting type cast errors & similar, I registered the interceptors as virtual method interceptors instead of interface interceptors for the mapped types. I believe this is to be expected given how the proxies work --   

Jimmy Bogard

unread,
Feb 3, 2015, 12:14:19 PM2/3/15
to automapp...@googlegroups.com
I'll have to give this a try locally too - I don't do a lot of stuff with AutoMapper and interceptors. Well, none really. It does sound like a bug, I need to do some prototyping to see if I can reproduce it easily.

Ca Hy

unread,
Feb 3, 2015, 3:59:53 PM2/3/15
to automapp...@googlegroups.com
If I have the chance tonight, I'll  get a demo app together for it -- might be a couple of days before I get a chance. 

Ca Hy

unread,
Feb 4, 2015, 9:22:26 AM2/4/15
to automapp...@googlegroups.com

I was able to put together a simple c# demo app last night that demonstrates what I am seeing. My primary goal was to make it as easy as possible for you to help me out. :) 


I'm not a regular GitHub uploader, so if it's not right, please let me know. 
Reply all
Reply to author
Forward
0 new messages