Question

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.Registrations property). 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 IUnityContainer interface, 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.

Was it helpful?

Solution

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:

  container.RegisterType<ISomething>(
    new InjectionFactory(c => {
        var newObject = (Something)Weaver.CreateInstance(typeof(Something));
        newObject.Property1 = c.Resolve<TypeOfProperty1>();
        newObject.Property2 = c.Resolve<TypeofProperty2>();
        return newObject;
    });

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.

OTHER TIPS

A glance at the LOOM codeplex page doesn’t appear to provide any features that can’t be done using Unity method interception. Start reading here: Aspect-Oriented Programming, Interception and Unity 2.0

this is a poor argument. It's the same if I say Microsoft's Unity doesn't provide any features that can't be done with the .NET-framework. The question is, what is the best programming model for my problem. A answer could be the amount of code, that need to implement the requirements. AOP, especially Rapier-LOOM.NET is not a simple method inception facility. The goal of AOP is to encapsulate crosscutting concerns. For that you need advices, introductions, join-point variables, code-based annotations etc. If I want to implement more than a simple Tracing example you need more powerful concepts than method inception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top