Hi

Firstly I wanted to report my thoughts in http://issues.castleproject.org/ 
as a bug description, but the page doesn't work - it only redirects to 
github 'castle-youtrack-export' page, so need to discuss the issue here.

Scenario:
I use some different types of, let say, 'ViewModels', where all of 
ViewModel-types use some services but with different implementations. The 
natural way of implement this is register all available services 
implementations and resolve them per ViewModel instance

I believe that the unit test below describes my idea and expectation. 
Please run the test - it fails.

    [TestFixture]
    public sealed class BoundedLifestyleTests
    {
        class ViewModel
        {
            public IService[] Services { get; set; }
        }

        public interface IService { }
        class NeutralStrategy : IService { }
        class SpecializedStrategy : IService { }

        /// <summary>
        /// I have some IService implementations, where the are either are 
neutral (could be used in any context)
        /// or are specialized (should be resolved only in ViewModel).
        /// I would like to implement that requirement with 
LifestyleBoundTo which looks promising.
        /// </summary>
        [Test]
        public void Test()
        {
            var container = new WindsorContainer();
            container.Kernel.Resolver.AddSubResolver(new 
CollectionResolver(container.Kernel, true));

            container.Register(
                Component.For<IService>().ImplementedBy<NeutralStrategy>(),
                Component.For<ViewModel>(),
                
Component.For<IService>().ImplementedBy<SpecializedStrategy>().LifestyleBoundTo<ViewModel>());

            // ViewModel has neutral and specialized strategies resolved.
            var viewModel = container.Resolve<ViewModel>();
            Assert.That(viewModel.Services.Select(o => o.GetType()), 
Is.EquivalentTo(new[] { typeof(NeutralStrategy), 
typeof(SpecializedStrategy) }));

            // Outside of a graph bound to ViewModel, we still should be 
able to resolve neutral strategy.
            var neutralStrategies = container.ResolveAll<IService>();
            Assert.That(neutralStrategies.Select(o => o.GetType()), 
Is.EquivalentTo(new[] { typeof(NeutralStrategy) }));
        }
    }

Regards

Slawek

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to castle-project-users+unsubscr...@googlegroups.com.
To post to this group, send email to castle-project-users@googlegroups.com.
Visit this group at http://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to