Why does my custom BuilderStrategy return null when I'm setting Existing property

Jun 11, 2011 at 8:18 PM
        class EnumerableStrategy : BuilderStrategy
        {
            public override void PreBuildUp(IBuilderContext context)
            {
                context.Existing = new []{"Test"};
                context.BuildComplete = true;
            }
        }

        class EnumerableExtension : UnityContainerExtension
        {
            protected override void Initialize()
            {
                Context.BuildPlanStrategies.AddNew<EnumerableStrategy>( Microsoft.Practices.Unity.ObjectBuilder.UnityBuildStage.PreCreation);
            }
        }
        static void Main(string[] args)
        {
           
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<EnumerableExtension>();
            var items = container.Resolve<IEnumerable<string>>();
            foreach (var item in items)
                Console.WriteLine(item.ToString());
        }


Items variable turns out to be null. Why?
Jun 14, 2011 at 8:14 AM

Try using the following instead

Context.Strategies.AddNew<EnumerableStrategy>(UnityBuildStage.PreCreation);

HTH

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jun 14, 2011 at 8:22 AM

Works! thanks. do you know the difference between BuildPlanStrategies and Strategies?

Jun 14, 2011 at 9:49 AM

I've seen that Chris Tavares (part of the team developed Unity and Entlib) have already answered you on this in Stackoverflow discussion - http://stackoverflow.com/questions/6318015/why-does-unity-custom-builderstrategy-returns-null-even-after-setting-existing-pr. Hope this answers your question already.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com