ResolveTrampoline<TItem> implementation

Feb 12, 2012 at 6:03 PM
Edited Feb 12, 2012 at 6:06 PM

Hi,

Correct me if I'm wrong, but I'm under the impression that Func<T> deferred resolution is, by design, supposed to work as similarly as possible to traditional type resolution. However, the resolution of the deferred dependency in ResolveTrampoline<TItem>.Resolve (ObjectBuilder\Strategies\BuildPlan\Factory\DeferredResolveBuildPlanPolicy.cs) is done via a completely independent "external" call to the container, that drops any notion of the current build context. Therefore, a lot of functionalities are lost. For example, this test fixture will fail:

 

[TestMethod]
public void HonoursPerResolveLifetimeManagerWhenResolvingFunc()
{
    var container = new UnityContainer()
        .RegisterType<ILogger, MockLogger>(new PerResolveLifetimeManager());

    var resolver = container.Resolve<Func<ILogger>>();
    var result1 = resolver();
    var result2 = resolver();

    Assert.AreSame(result1, result2);
}

 

It is possible to get this test to pass, along with the 977 other tests, by modifying the ResolveTrampoline to accept the build context, and calling IBuilderContext.NewBuildUp rather than IUnityContainer.Resolve. Do you see any contraindication that I should be aware of ? I suspect there is a good reason why it has not been done by the developers, but I can't see it.

Thanks in advance