For DI I’m using Microsoft’s Unity.
For dynamic Aspect Weaving I’m using Rapier-LOOM.
The aspect weaver requires me to instantiate woven objects using the factory method Weaver.CreateInstance(System.Type), and provides no means of interweaving an existing instance.
The DI container allows me to resolve dependencies by using IUnityContainer.Resolve(System.Type) method, which resolves the dependency and also instantiates an object of the injected type.
These two approaches obviously conflict.
What would be a recommended way to resolve this conflict?
Ideas I’ve had so far:
- Inquire the mapping and “manually resolve” the dependency (using the
IUnityContainer.Registrationsproperty). Create a combined “DI+AOP” mechanism which – given a type to resolve – finds the target mapped type then instantiates using the Weaver. - Create my own implementation of the
IUnityContainerinterface, which instantiates using the Weaver (instead of the Activator)
P.S.
If I’m off-track here and the conflict could be avoided rather than resolved – please let me know.
I’m not familiar with Rapier-LOOM, so I’ll just talk from the Unity side of things. There are a couple approaches of varying capability / complexity. None of them involve reimplementing IUnityContainer, luckily.
The simplest thing you could do is register types you want created via the weaver using InjectionFactory. This lets you specify a delegate that will be executed to create the instance instead of the default behavior. Something like this:
Then when you call container.Resolve(), that delegate will run.
The second approach would be to create a Unity extension that hooks in the Weaver.CreateInstance call into the creation chain. You could either use a custom strategy in the main strategy chain, or try overriding the build plan. The former is much easier.
I don’t have my references for creating Unity extensions at hand, so I’m not going to try to type code into this textbox right now. Look around the web for examples of Unity extensions, they’re pretty straightforward once you understand how things go together.