﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Francois Tanguay commented on Service locator for optional dependencies</title><description>Given, it hides dependencies. They aren't enforced.
  
  
So are method calls on dynamic languages.
  
  
For dynamic languages, people argue that it is good test coverage that ensures success...
  
  
Couldn't it be the same thing for  service locator?
  
  
There are some pros and cons to any approach. 
  
  
Cons of Service Locator are definitely related to how explicit are the dependencies.
  
  
Pros are lazy creation of depencies (I find app initialization time to be generaly better)
  
  
Also, I've been using extensions methods on IServiceLocator to ease cross-cutting concern usage.
  
  
e.g.
  
interface IServiceLocator { T Resolve&lt;T&gt;(); }
  
interface IServiceLocatorClient { IServiceLocator Locator { get; set; }}
  
  
public static class LoggerExtensions
  
{
  
  public static void Log(this IServiceLocatorClient client, string msg)
  
  {
  
    client.Locator.Resolve&lt;ILogger&gt;().Log(msg);
  
  }
  
}
  
  
With IoC:
  
public class Foo : IServiceLocatorClient
  
{
  
  public IServiceLocator { get; set; } //THE only dependency that has to be injected.
  
  
  void Do() 
  
  {
  
    try { ... } 
  
   catch (Exception ex) { this.Log(exception.Message); }
  
  }
  
}
</description><link>http://ayende.com/3094/service-locator-for-optional-dependencies#comment9</link><guid>http://ayende.com/3094/service-locator-for-optional-dependencies#comment9</guid><pubDate>Sun, 13 Jan 2008 17:30:39 GMT</pubDate></item><item><title>Ayende Rahien commented on Service locator for optional dependencies</title><description>Francois,
  
I _really_ don't like this approach. It is opaque, it hides the dependencies and it makes it easier to fail late rather than early.
</description><link>http://ayende.com/3094/service-locator-for-optional-dependencies#comment8</link><guid>http://ayende.com/3094/service-locator-for-optional-dependencies#comment8</guid><pubDate>Sun, 13 Jan 2008 15:41:51 GMT</pubDate></item><item><title>Francois Tanguay commented on Service locator for optional dependencies</title><description>Ok, I probably don't have the same definition of "optional" dependency.
  
  
Let's say logging is something I just do when exceptions are thrown. If exceptions never happen, I don't need an instance of ILogger.
  
  
With Ioc, dependencies are resolved at config time. WIth service locator, they are resolved at runtime, on demand.
  
  
Ioc:
  
try { ...}
  
catch (Exception ex) { MyInjectedLogger.Log(ex); }
  
  
ServiceLocator:
  
try { ...}
  
catch (Exception ex) { SerivceLocator.Resolve&lt;ILogger&gt;.Log(ex); }
</description><link>http://ayende.com/3094/service-locator-for-optional-dependencies#comment7</link><guid>http://ayende.com/3094/service-locator-for-optional-dependencies#comment7</guid><pubDate>Sun, 13 Jan 2008 15:07:52 GMT</pubDate></item><item><title>Ayende Rahien commented on Service locator for optional dependencies</title><description>Francois,
  
Absolutely not.
  
If I want to use optional dependencies using DI (like I should), I just put a property.
  
  
This is for infrastructure concerns more than anything else
</description><link>http://ayende.com/3094/service-locator-for-optional-dependencies#comment6</link><guid>http://ayende.com/3094/service-locator-for-optional-dependencies#comment6</guid><pubDate>Sat, 12 Jan 2008 15:42:46 GMT</pubDate></item><item><title>Francois Tanguay commented on Service locator for optional dependencies</title><description>If I understand correcly, you end up with two different mechanisms to get your dependencies  depending whether they're optional or not.
  
  
That's why I prefer to fo with a service locator all the way.
</description><link>http://ayende.com/3094/service-locator-for-optional-dependencies#comment5</link><guid>http://ayende.com/3094/service-locator-for-optional-dependencies#comment5</guid><pubDate>Sat, 12 Jan 2008 15:31:21 GMT</pubDate></item><item><title>Ayende Rahien commented on Service locator for optional dependencies</title><description>It is used for service location, mostly.
  
I don't like it, but it is really useful when you want to use IoC on things like static classes.
</description><link>http://ayende.com/3094/service-locator-for-optional-dependencies#comment4</link><guid>http://ayende.com/3094/service-locator-for-optional-dependencies#comment4</guid><pubDate>Sat, 12 Jan 2008 11:07:19 GMT</pubDate></item><item><title>Andrey Shchekin commented on Service locator for optional dependencies</title><description>Can you explain the use cases of IoC.Resolve&lt;&gt;?
  
The thing I do not completely understand is which classes get their dependencies through dependency injection and which use Resolve?
</description><link>http://ayende.com/3094/service-locator-for-optional-dependencies#comment3</link><guid>http://ayende.com/3094/service-locator-for-optional-dependencies#comment3</guid><pubDate>Sat, 12 Jan 2008 10:25:03 GMT</pubDate></item><item><title>Ayende Rahien commented on Service locator for optional dependencies</title><description>Joshua,
  
Yes, it would.
  
The problem is that I am writing a library, and I don't want to force the users of the library to define certain things on the container.
</description><link>http://ayende.com/3094/service-locator-for-optional-dependencies#comment2</link><guid>http://ayende.com/3094/service-locator-for-optional-dependencies#comment2</guid><pubDate>Sat, 12 Jan 2008 09:03:47 GMT</pubDate></item><item><title>Joshua McKinney commented on Service locator for optional dependencies</title><description>I'm not sure I understand. Wouldn't it have been better to just set put NullLogger in the IoC container in the first place? Doesn't this kinda defeat the purpose of IoC? Perhaps better would be to extend the container much like the auto mocking container to provide a NullService for non resolvable dependencies.
</description><link>http://ayende.com/3094/service-locator-for-optional-dependencies#comment1</link><guid>http://ayende.com/3094/service-locator-for-optional-dependencies#comment1</guid><pubDate>Sat, 12 Jan 2008 08:25:14 GMT</pubDate></item></channel></rss>