is there a way to know DependencyInjectionAreDone?

Apr 5, 2012 at 3:56 AM

I want to do something after the injections of a class is done.

i'm using method injection.

say:

MyClass{
  [InjectionMethod]
  public void Inject(a....b) {}

  void X(){
   if (this.UnityInjectionsAreDone) DoThat();
  }
}

instead of checking those dependepency whether they are null, is there a build-in event/flag to indicate the injection operation is done?
Also not intend to do the DoThat() within Inject() at last execution.
I won't know when other may introduce their own injection.
Apr 6, 2012 at 10:00 AM
Edited Apr 6, 2012 at 4:16 PM

There is no out of the box way to know when all of the injections have occurred.  What you could do is use a UnityContainerExtension.  First create an interface so we can tell the class that method injection is completed:

    public interface IUnityMethodInjectionCompleted
    {
        void MethodInjectionComplete();
    }

    public class MyClass : IUnityMethodInjectionCompleted
    {
        private bool areUnityInjectionsDone;

        public void MethodInjectionComplete()
        {
            areUnityInjectionsDone = true;
            DoThat();
        }

        [InjectionMethod]
        public void InjectB()
        {
        }

        [InjectionMethod]
        public void InjectA()
        {
        }

        public void DoThat()
        {
        }
    }

Then create the extension to indicate method injection is done:

    public class MyContainerExtension : UnityContainerExtension
    {
        protected override void Initialize()
        {
            var strategy = new MyBuilderStrategy(Container);

            Context.Strategies.Add(strategy, UnityBuildStage.Initialization);
        }

        class MyBuilderStrategy : BuilderStrategy
        {
            private readonly IUnityContainer container;

            public MyBuilderStrategy(IUnityContainer container)
            {
                this.container = container;
            }

            public override void PreBuildUp(IBuilderContext context)
            {
                var obj = context.Existing as IUnityMethodInjectionCompleted;

                if (obj != null)
                {
                    obj.MethodInjectionComplete();
                }
            }
        }
    }

Then register the extension with the container:

    var container = new UnityContainer();
    container.AddNewExtension<MyContainerExtension>();

    var myClass = container.Resolve<MyClass>();

Now you should be able to know when method injection is complete and perform some action.

I'm not familiar with your design so it's hard to judge but based on your posting I just have to wonder if there is a better design to achieve what you want.  Perhaps a decorator pattern or another approach?  By using Unity to determine when injection is complete you are coupling the behavior (and perhaps design) to Unity.  Something to think about.

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