tag:blogger.com,1999:blog-15136575.post1694410246659725140..comments2023-10-17T12:00:16.772+01:00Comments on Code rant: Using ServiceLocator in an MVC Framework applicationMike Hadlowhttp://www.blogger.com/profile/16441901713967254504noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-15136575.post-50979487753448946362008-10-16T17:58:00.000+01:002008-10-16T17:58:00.000+01:00Absolutely. They should add ServiceLocator.SetLoca...Absolutely. They should add ServiceLocator.SetLocator(IServiceLocator).<BR/><BR/>Other than that, it's a fine API :)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15136575.post-57375961416158444652008-10-16T09:11:00.000+01:002008-10-16T09:11:00.000+01:00Bruce,Yes, I saw your comments on this on the Code...Bruce,<BR/><BR/>Yes, I saw your comments on this on the Codeplex site.<BR/><BR/>It's a very good point. <BR/><BR/>Looking agian at my code above, although it will not create a new container every time, it will create a new WindsorServiceLocator. That's not what I want. I should probably written this:<BR/><BR/>var serviceLocator = new WindsorServiceLocator(container);<BR/>ServiceLocator.SetLocatorProvider(() => serviceLocator);<BR/><BR/>Functional style lazy evaluation is a very powerful technique, but you are right that it doesn't make for a very obvious API. Maybe a SetLocatorProvider overload that just took a ServiceLocator is needed. What do you think?Mike Hadlowhttps://www.blogger.com/profile/16441901713967254504noreply@blogger.comtag:blogger.com,1999:blog-15136575.post-48056389442082701162008-10-15T20:00:00.000+01:002008-10-15T20:00:00.000+01:00One issue I have with the CSL interface is the fac...One issue I have with the CSL interface is the fact that you set the service locator by passing a delegate or lambda. I can see the reasoning behind it but it can be a source of subtle bugs.<BR/><BR/>The difference between<BR/><BR/>var container = new WindsorContainer(new XmlInterpreter(new ConfigResource("components")));<BR/>ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));<BR/><BR/>and<BR/><BR/>ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(new WindsorContainer(new XmlInterpreter(new ConfigResource("components"))));<BR/><BR/>is subtle, but in the second example, each call to ServiceLocator.Current news up a new container. This causes havoc, especially with services with singleton lifestyles.<BR/><BR/>As a newcomer to IoC, this is just another potential pitfall which could easily be avoided with better documentation or a better API.Anonymousnoreply@blogger.com