API Reference

IServiceLocator

/// <summary>
/// The generic Service Locator interface. This interface is used
/// to retrieve services (instances identified by type and optional
/// name) from a container.
/// </summary>
public interface IServiceLocator : IServiceProvider
{
  /// <summary>
  /// Get an instance of the given <typeparamref name="TService"/>.
  /// </summary>
  /// <typeparam name="TService">Type of object requested.</typeparam>
  /// <param name="implementationKey">Name the object was registered with.</param>
  /// <exception cref="ActivationException">if there is are errors resolving
  /// the service instance.</exception>
  /// <returns>The requested service instance.</returns>
  TService GetInstance<TService>(string implementationKey = null);

  /// <summary>
  /// Get an instance of the given <paramref name="serviceType"/>.
  /// </summary>
  /// <param name="serviceType">Type of object requested.</param>
  /// <param name="implementationKey">Name the object was registered with.</param>
  /// <exception cref="ActivationException">if there is an error resolving
  /// the service instance.</exception>
  /// <returns>The requested service instance.</returns>
  object GetInstance(Type serviceType, string implementationKey = null);

  /// <summary>
  /// Get all instances of the given <typeparamref name="TService"/> currently
  /// registered in the container.
  /// </summary>
  /// <typeparam name="TService">Type of object requested.</typeparam>
  /// <exception cref="ActivationException">if there is are errors resolving
  /// the service instance.</exception>
  /// <returns>A sequence of instances of the requested <typeparamref name="TService"/>.</returns>
  IEnumerable<TService> GetAllInstances<TService>();

  /// <summary>
  /// Get all instances of the given <paramref name="serviceType"/> currently
  /// registered in the container.
  /// </summary>
  /// <param name="serviceType">Type of object requested.</param>
  /// <exception cref="ActivationException">if there is are errors resolving
  /// the service instance.</exception>
  /// <returns>A sequence of instances of the requested <paramref name="serviceType"/>.</returns>
  IEnumerable<object> GetAllInstances(Type serviceType);
}

IServiceConfigurator

/// <summary>
/// The service that makes our different project parts configurable for Dependency Injection and Inversion of Control.
/// </summary>
public interface IServiceConfigurator
{
  /// <summary>
  /// Access to DI configure a type <typeparamref name="TToConfigure"/>
  /// </summary>
  /// <typeparam name="TToConfigure">The implementation to configure.</typeparam>
  /// <param name="implementationKey">The key when configuring multiple implementations of the same contract.</param>
  /// <returns>A <see cref="IConfigurator{TToConfigure}"/> that enables further configuration on the <typeparamref name="TToConfigure"/></returns>
  /// <exception cref="ConfigurationException">if there is are errors configuring the <typeparamref name="TToConfigure"/>.</exception>
  IConfigurator<TToConfigure> Configure<TToConfigure>(string implementationKey = null);

  /// <summary>
  /// Register a <typeparamref key="TImplementation"/> as implementing <typeparamref key="TContract"/>.
  /// </summary>
  /// <typeparam name="TContract">The contract to register as a service.</typeparam>
  /// <typeparam name="TImplementation">The implementation that supports this contract.</typeparam>
  /// <param name="implementationKey">The key when registering multiple implementations of the same contract.</param>
  /// <returns>A <see cref="IConfigurator{TImplementation}"/> that enables further configuration on the <typeparamref key="TImplementation"/></returns>
  /// <exception cref="ConfigurationException">if there is are errors configuring the <typeparamref name="TImplementation"/>.</exception>
  IConfigurator<TImplementation> RegisterType<TContract, TImplementation>(string implementationKey = null)
    where TImplementation : TContract;

  /// <summary>
  /// Register an instance of <typeparamref key="TContract"/> as the (shared) service that supports this contract
  /// </summary>
  /// <typeparam name="TContract">The contract to register as a service.</typeparam>
  /// <param name="implementationKey">The key when registering multiple implementations of the same contract.</param>
  /// <param name="instance">The instance of <typeparamref key="TContract"/> to register</param>
  /// <exception cref="ConfigurationException">if there is are errors configuring the <typeparamref name="TContract"/>.</exception>
  void RegisterInstance<TContract>(TContract instance, string implementationKey = null);

  /// <summary>
  /// Register an instance of <paramref key="instance"/> as the (shared) service that supports this contract
  /// </summary>
  /// <param name="instance">The instance of <paramref key="instance"/> to register</param>
  /// <param name="contractType">The type to register as.</param>
  /// <param name="implementationkey">The key when registering multiple implementations of the same contract.</param>
  /// <exception cref="ConfigurationException">if there is are errors configuring the <paramref name="contractType"/>.</exception>
  void RegisterInstance(object instance, Type contractType, string implementationkey = null);
}

IConfigurator<TImplementation>

/// <summary>
/// The configurator enables configuration options for one type in the underlying IoC Container. Each method reaturns this making method chaining possible.
/// </summary>
/// <typeparam name="TImplementation">The <see cref="Type"/> we are configuring.</typeparam>
public interface IConfigurator<TImplementation>
{
  /// <summary>
  /// The [optional] name of the implementation to configure
  /// </summary>
  string Name { get; }

  /// <summary>
  /// Set the <typeparamref name="TImplementation"/> as singleton instance in the application.
  /// </summary>
  /// <returns>This <see cref="IConfigurator{TImplementation}"/> for a fluent interface.</returns>
  /// <exception cref="ConfigurationException">if there is are errors configuring the <typeparamref name="TImplementation"/>.</exception>
  IConfigurator<TImplementation> AsSingleton();

  /// <summary>
  /// Use a type-safe expression to select the constructor to use for Dependency Injection.
  /// </summary>
  /// <param name="constructionExpression">An <see cref="Expression{TDelegate}"/> which will be parsed.</param>
  /// <param name="sequentialImplementationKeys">If any of the parameter types in the constructor has more than one implementation this may also be defined.</param>
  /// <exception cref="MalformedConfigurationExpressionException">Exception is thrown if the <see cref="Expression{TDelegate}"/> is not parsable as an expression that returns a new instance of <typeparamref name="TImplementation"/></exception>
  /// <exception cref="ConfigurationException">if there is are errors configuring the <typeparamref name="TImplementation"/>.</exception>
  /// <remarks>The expressions will be parsed only - never evalutated.</remarks>
  /// <remarks>The <paramref name="sequentialImplementationKeys"/> will not be type safe during refactoring and mark sequentially the (optional) implementation key used to register a speciffic implementation of a type that is a parameter to the constructor selected for Dependency Injection.</remarks>
  /// <returns>This <see cref="IConfigurator{TImplementation}"/> for a fluent interface.</returns>
  IConfigurator<TImplementation> InjectionConstructor(Expression<Func<TImplementation>> constructionExpression, params string[] sequentialImplementationKeys);

  /// <summary>
  /// Select properties the DI container should choose when injecting the instance of <typeparamref name="TImplementation"/>
  /// </summary>
  /// <param name="injectionPropertyExpressions">Lambda expressions defining in a strongly typed manner the property member that is to be marked for property injection</param>
  /// <remarks>The expressions will be parsed only - never evaluated.</remarks>
  /// <exception cref="MalformedConfigurationExpressionException">Exception is thrown if the <see cref="Expression{TDelegate}"/> is not parsable as an expression that selects a property on an instance of <typeparamref name="TImplementation"/></exception>
  /// <exception cref="ConfigurationException">if there is are errors configuring the <typeparamref name="TImplementation"/>.</exception>
  /// <returns>This <see cref="IConfigurator{TImplementation}"/> for a fluent interface.</returns>
  IConfigurator<TImplementation> InjectionProperties(params Expression<Func<TImplementation, object>>[] injectionPropertyExpressions);

  /// <summary>
  /// Use type-safe expressions to select properties for Dependency Injection. Pair each up with an implmementation key when a property has more than one implementation in the system.
  /// </summary>
  /// <param name="keyedPropertyInjectionPropertyExpressions">The expressions that each selects a property for dependency injection together with an implementation key for the <see cref="Type"/> of the property.</param>
  /// <remarks>The expressions will be parsed only - never evalutated.</remarks>
  /// <exception cref="MalformedConfigurationExpressionException">Exception is thrown if the <see cref="Expression{TDelegate}"/> is not parsable as an expression that selects a property on an instance of <typeparamref name="TImplementation"/></exception>
  /// <exception cref="ConfigurationException">if there is are errors configuring the <typeparamref name="TImplementation"/>.</exception>
  /// <returns>This <see cref="IConfigurator{TImplementation}"/> for a fluent interface.</returns>
  IConfigurator<TImplementation> InjectionProperties(params KeyedPropertyExpression<TImplementation>[] keyedPropertyInjectionPropertyExpressions);
}

ConfiguratorBase<TImplementation>

/// <summary>
/// Base class for <see cref="IConfigurator{TImplementation}"/> that implements the expression based members that end up calling their non-expression based counterparts.
/// </summary>
/// <typeparam name="TImplementation">The type to configure dependency injection for</typeparam>
public abstract class ConfiguratorBase<TImplementation> : IConfigurator<TImplementation>
{
  /// <summary>
  /// <see cref="IConfigurator{TImplementation}.AsSingleton()"/>
  /// </summary>
  public abstract IConfigurator<TImplementation> AsSingleton();

  /// <summary>
  /// Implement this to enable the <see cref="InjectionConstructor(Expression{Func{TImplementation}},string[])"/> method to call this adapted simplified version.
  /// </summary>
  /// <param name="constructionExpressions">The (<see cref="String"/>)key/(<see cref="Type"/>) pairs that make up constructor parameters types and optional names for the implementations.</param>
  /// <returns>this</returns>
  protected abstract IConfigurator<TImplementation> InjectionConstructor(params KeyedType[] constructionExpressions);

  /// <summary>
  /// Implement this to enable the <see cref="InjectionProperties(Expression{Func{TImplementation,object}}[])"/> method to call this adapted simplified version.
  /// </summary>
  /// <param name="keyedPropertyInjectionPropertyExpressions">The (<see cref="String"/>)key/(<see cref="String"/>Name) pairs that make up property names and optional keys.</param>
  /// <returns>this</returns>
  protected abstract IConfigurator<TImplementation> InjectionProperties(params KeyedName[] keyedPropertyInjectionPropertyExpressions);
  
  // [...]
}

Last edited Sep 2, 2010 at 8:45 PM by noopman, version 9

Comments

No comments yet.