Hi, I run your code with multiple combination and all fails at resolving all IService. I saw on few posts on QA forums that programmers mostly resolve bounded to objects only (ex. ViewModels).
Always better inject than service locate. http://docs.castleproject.org/Windsor.LifeStyles.ashx Try some other life style maybe it will fix your problem. W dniu piątek, 23 stycznia 2015 00:58:15 UTC+1 użytkownik Koke napisał: > > well maybe its not qualify how beautiful code but work > > Named each implementation and using "DependsOn" register it > > a better approach be register all implementation > > container.Register(Classes.FromThisAssembly() > .BasedOn<IService>() > .LifestyleTransient() > ); > > and on depends set it, but i dont know > > container.Register( > Component.For<ViewModel>() > > .DependsOn(Dependency.OnComponentCollection("Services", new []{ > typeof(IService) })) > ); > > > Here the working example > > > [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(Classes.FromThisAssembly() > // .BasedOn<IService>() > // .LifestyleTransient() > // ); > > container.Register( > Component.For<IService>() > .ImplementedBy<NeutralStrategy>() > .Named("se1") > .LifeStyle.Transient > ); > > container.Register( > Component.For<IService>() > .ImplementedBy<SpecializedStrategy>() > .Named("se2") > .LifeStyle.Transient > ); > > container.Register( > Component.For<ViewModel>() > > .DependsOn(Dependency.OnComponentCollection("Services", new > string[]{"se1","se2"})) > > //.DependsOn(Dependency.OnComponentCollection("Services", new []{ > typeof(IService) })) > ); > > // ViewModel has neutral and specialized strategies resolved. > var viewModel = container.Resolve<ViewModel>(); > //Console.WriteLine("viewmodel can " + > viewModel.Services.Count); > 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), > typeof(SpecializedStrategy) } )); > } > } > > > regards > > 2015-01-22 15:04 GMT-06:00 Sławomir Siudek <[email protected] <javascript:> > >: > >> 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 [email protected] <javascript:>. >> To post to this group, send email to [email protected] >> <javascript:>. >> Visit this group at http://groups.google.com/group/castle-project-users. >> For more options, visit https://groups.google.com/d/optout. >> > > -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/castle-project-users. For more options, visit https://groups.google.com/d/optout.
