views:

665

answers:

2

I am using the Unity IoC container, and I need to intercept any calls to Resolve for a certain base interface, and run my own custom code to construct those types.

In other words, in the sample code below, when I call container.Resolve<IFooN>(), if it hasn't got an instance of the concrete implementing type, it calls MyFactoryFunction to construct one, otherwise I want it to return the cached copy.

The standard Unity container is not able to construct these objects (update: because they are .NET remoting objects, so the concrete classes do not exist in any assembly on the local computer), and I don't want to create them up front and store them with RegisterInstance.

interface IFoo : IBase { ... }
interface IFoo2 : IBase { ... }

...
container.Resolve<IFoo2>();

...
IBase MyFactoryFunction(Type t)
{
    ...
}

I'm assuming I can create a Unity extension to do this, but I was wondering if there is already a solution out there I can borrow.

A: 

The Unity container already acts as a factory that knows how to construct (and perform dependency injection for) arbitrary types, so your factory that accepts a Type t appears redundant. Can you elaborate more on why this is not possible?

If this is truly not possible (more likely just too much work), then perhaps you can register your factory with the container instead?

Jerry Bullard
the reason is that we are using .NET remoting to create the class. So we can't simply call its constructor - the concrete type does not exist in any assembly on the client. We have to connect to a server, and request its object that fulfils the interface in question.
Mark Heath
+3  A: 

OK, I have implemented the extension myself. In the builder I cache the object as I want it to be a singleton w.r.t my container. The reason for baseContext is that I want it to be cached in the top level container rather than in any child containers from which it was requested.

 public class FactoryMethodUnityExtension<T> : UnityContainerExtension
 {
     private Func<Type,T> factory; 

     public FactoryMethodUnityExtension(Func<Type,T> factory)
     {
         this.factory = factory;
     }

     protected override void Initialize()
     {
         var strategy = new CustomFactoryBuildStrategy<T>(factory, this.Context);
         Context.Strategies.Add(strategy, UnityBuildStage.PreCreation);             
     }
 } 

 public class CustomFactoryBuildStrategy<T> : BuilderStrategy
 {
     private Func<Type,T> factory;
     private ExtensionContext baseContext;

     public CustomFactoryBuildStrategy(Func<Type,T> factory, ExtensionContext baseContext)
     {
        this.factory = factory;
        this.baseContext = baseContext;
     }

     public override void PreBuildUp(IBuilderContext context)
     {
         var key = (NamedTypeBuildKey)context.OriginalBuildKey;

         if (key.Type.IsInterface && typeof(T).IsAssignableFrom(key.Type))
         {
             object existing = baseContext.Locator.Get(key.Type);
             if (existing == null)
             {
                 // create it
                 context.Existing = factory(key.Type);
                 // cache it
                 baseContext.Locator.Add(key.Type, context.Existing);
             }
             else
             {
                 context.Existing = existing;
             }
         }
     }
 }

Adding the extension is quite simple:

MyFactory factory = new MyFactory();
container = new UnityContainer();
container.AddExtension(new FactoryMethodUnityExtension<IBase>(factory.Create));
Mark Heath