Unity container: using PerResolveLifetimeManager and custom interception

I am trying to implement my own interception when using a Unity container. I want to do this in such a way that he respects the life manager used. that is, if it is a PerResolveLifetimeManager, then I want one instance to be completed, and I want this wrapped instance to be used in the whole solution.

So far, I have implemented BuilderStrategy, which I am adding to my container using my own UnityContainerExtension class (I pass PostInitialization to AddNew method, I'm not sure what the most suitable value is, but it seems to have worked). In overriding PostBuildUp in my BuilderStrategy, I replace context.Existing with my wrapped value.

When I use this with the PerResolve lifetime, one transfer occurs, but only the first use of the dependencies gets the wrapped instance and the rest the non-integrated instance. that is, if my ctor accepts IFoo foo1 and IFoo foo2, then only foo1 is my wrapped instance, while foo2 is a deployed instance.

Here is a playback example (for simplicity, I use an instance of another Foo2 class instead of wrapping):

public class MyInterception : UnityContainerExtension { protected override void Initialize() { Context.Strategies.AddNew<MyInterceptionStrategy>(UnityBuildStage.PostInitialization); } } public class MyInterceptionStrategy : BuilderStrategy { public override void PostBuildUp(IBuilderContext context) { if (!context.OriginalBuildKey.Type.IsInterface) { return; } context.Existing = new Foo2(); } } public class Bar { public Bar(IFoo f1, IFoo f2, IFoo f3) { } } public interface IFoo { } public class Foo1 : IFoo { } public class Foo2 : IFoo { } public void Main() { UnityContainer container = new UnityContainer(); container.AddNewExtension<MyInterception>(); container.RegisterType(typeof (IFoo), typeof (Foo1), new PerResolveLifetimeManager()); container.Resolve<Bar>(); } 

If you put a breakpoint in PostBuildUp and in the Bar contructor and debug Main up with Bar permission, you will see that PostBuildUp is called only once. However, in the constructor of Bar, f1 is an instance of Foo2 (a wrapped instance), while f2 and f3 are instances of Foo1 (expand).

I was wondering if what I want is possible and if I fit right? Are my issues related to the lifetimes and / or UnityBuildStage for which I added BuilderStrategy?

thanks

+7
c # unity-container
source share
1 answer

You use the build strategy interception strategy too late. Unfortunately, at the moment I have no more detailed reason why the code behaves this way inside the ObjectBuilder assembly.

Hopefully I will be back when I have more time to analyze ObjectBuilder Unity, but to solve the problem in a while, just change the UnityBuildStage enumeration from PostInitialization to Setup or TypeMapping

 Context.Strategies.AddNew<MyInterceptionStrategy>(UnityBuildStage.Setup); 

My full code is:

 using System; using Microsoft.Practices.ObjectBuilder2; using Microsoft.Practices.Unity; using Microsoft.Practices.Unity.ObjectBuilder; namespace UnityInterceptors { class Program { public class MyInterception : UnityContainerExtension { protected override void Initialize() { Context.Strategies.AddNew<MyInterceptionStrategy>(UnityBuildStage.Setup); } } public class MyInterceptionStrategy : BuilderStrategy { public override void PostBuildUp(IBuilderContext context) { if (!context.OriginalBuildKey.Type.IsInterface) { return; } context.Existing = new Foo2(); } } public class Bar { public Bar(IFoo f1, IFoo f2, IFoo f3) { Console.WriteLine(f1.GetType().Name); Console.WriteLine(f2.GetType().Name); Console.WriteLine(f3.GetType().Name); } } public interface IFoo { } public class Foo1 : IFoo { } public class Foo2 : IFoo { } public static void Main() { UnityContainer container = new UnityContainer(); container.AddNewExtension<MyInterception>(); container.RegisterType(typeof(IFoo), typeof(Foo1), new PerResolveLifetimeManager()); container.Resolve<Bar>(); Console.ReadLine(); } } } 
  • Also note that I am using the Unity 2.0 library. Perhaps this was some error that was resolved.
+2
source share

All Articles