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