Unit's automatic abstract factory

Nov 30, 2012 at 8:14 PM
Edited Nov 30, 2012 at 8:18 PM

I'm new to Unity and I'm having a hard time trying to figure out how to expand Unity's concept of Auto Factory. Unity provides out of box the ability to create factories using Func in the place of the parameter itself. Like that:

public class Foo
{
    private readonly Func<IBar> _barFactory;

    public Foo(Func<IBar> barFactory)
    {
        _bar = barFactory;
    }
}

The thing is, there are places where the class created by the factory needs some parameters and I'll only know those parameters in run time. Other than that, the number and type of those parameters varies from class to class.

What I'm looking for is something similar to Autofac DelegateFacotry, but I would like to keep the Unity's feeling. So, I want to make Unity to work like that:

public class Foo
{
    private readonly Func<int, string, IBar> _barFactory;

    public Foo(Func<int, string, IBar> barFactory)
    {
        _bar = barFactory;
    }
}

The above code does not work, because Unity does work with that constructor, but it is exactly what I'm looking for.

I have tried use BuilderStrategy but it boils down to either Expression or IL generation. Before going down that path I would like to check for other options.

Is any one experienced enough with Unity who can help me with that?

Here are the constrains I have:

  1. Keep Unity's concept of using Func
  2. Do not need to register every single Func within the container
  3. The constructor should accept Func from Func<,> to Func<,,,,> (Func<T> is handled already)
  4. Changing the container is not an option
  5. Do not use abstract factories (as parameter)

I hope I was clear enough. If I was not, please let me know.

Nov 30, 2012 at 8:37 PM

This blog post describes an extension for Unity that generates delegate based factories. It will also work for Func<....>.

Nov 30, 2012 at 8:53 PM

Thanks for your answer weberse. It seems that solution will solve my problem. 

I noticed Unity latest bits has that functionality built in. Do you (or does anyone) know and could point me to the bits responsible for that in Unity's source code?

Nov 30, 2012 at 9:05 PM

The class responsible for creating the delegates is called DeferredResolveBuildPlanPolicy (namespace Microsoft.Practices.ObjectBuilder2).

Which latest bits do you mean? Using delegates with parameters did not work with Unity 2.0.

Nov 30, 2012 at 9:54 PM

You are right weberse. It worked because the code I run had a reference for a strongly typed solution I have written for test purposes.

Out of curiosity, "did not work with Unity 2.0" means that it does with Unity 3.0?

Nov 30, 2012 at 10:27 PM

I did a quick test and It doesn't appear to work with Unity 3.0 either.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Dec 1, 2012 at 5:47 AM

I didn't expect it to work with Unity 3.0. That feature is on the wishlist for Unity vNext.

Dec 1, 2012 at 3:48 PM

Thank you all guys for your help. I end up looking at Unity's source code (really appreciate the directions you gave me weberse) and coded an IBuildPlanPolicy which could handle the problem.