Latest Replies
Monday
Oct132008

Another alternative to the Single Service Locator Interface

Just to make my argument against powerful Common Service Locator Interface more constructive one.

A couple of highly specific interfaces and strongly-typed could be declared instead. It is easier to test these and they introduce smaller leak of control. Here is one set of the options:

/// <summary>
/// Interface that abstracts away providers
/// </summary>
/// <remarks>
/// things like IDataCache (from the Database layers) 
/// or IResolver (from the IoC layers) 
/// are just samples of this interface
/// </remarks>
public interface IProvider<TKey, TValue>
{
  /// <summary>
  /// Retrieves <typeparamref name="TValue"/> given the
  /// </summary>
  /// <param name="key"></param>
  /// <returns></returns>
  /// <exception cref="KeyInvalidException">when the key is invalid for
  /// the provider</exception>
  TValue Get(TKey key);
}

/// <summary>
/// Shortcut interface for <see cref="IProvider{TKey,TValue}"/> 
/// that uses <see cref="string"/> as the key.
/// </summary>
/// <typeparam name="TValue"></typeparam>
public interface INamedProvider<TValue> : IProvider<string, TValue>
{
}

/// <summary>
/// Generic resolution interface for the applications, 
/// where proper infrastructure could not be setup
/// </summary>
/// <remarks>There are no Generic resolution methods 
/// (like Resolve(Type service)), for the purpose of enforcing 
/// explicit resolution logics </remarks>
public interface IResolver
{
  /// <summary>
  /// Resolves this instance.
  /// </summary>
  /// <typeparam name="TService">The type of the service.</typeparam>
  /// <returns>requested instance of <typeparamref name="TService"/></returns>
  /// <exception cref="ResolutionException">
  /// if there is some resolution problem</exception>
  TService Get<TService>();

  /// <summary>
  /// Resolves the specified service type.
  /// </summary>
  /// <typeparam name="TService">The type of the service.</typeparam>
  /// <param name="name">The name.</param>
  /// <returns>
  /// requested instance of <typeparamref name="TService"/>
  /// </returns>
  /// <exception cref="ResolutionException">if there is 
  /// a resolution problem</exception>
  TService Get<TService>(string name);
}

IResolver is the interface that goes to the static class and delivers DI to code that could not get it otherwise.

The interfaces above (along with all the related code) could be found in the Lokad Shared Library.

« Writing .NET code analysis rules as unit tests | Main | Common Service Locator - one interface to rule them all »

Reader Comments (1)

[...] Common Service Locator breaks the SOC, by the way. Update: Alternative to CSL has been [...]

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>