Implements a simple inversion of control (IoC) container.

Namespace: DigitalRune.ServiceLocation
Assembly: DigitalRune.ServiceLocation (in DigitalRune.ServiceLocation.dll) Version: 1.0.0.0 (1.0.0.9503)

Syntax

C#
public class ServiceContainer : IServiceLocator, 
	IServiceProvider, IDisposable, IEnumerable
Visual Basic
Public Class ServiceContainer _
	Implements IServiceLocator, IServiceProvider, IDisposable, IEnumerable
Visual C++
public ref class ServiceContainer : IServiceLocator, 
	IServiceProvider, IDisposable, IEnumerable

Remarks

The ServiceContainer supports basic dependency injection:

  • Constructor Injection: When a new instance is created in GetInstance(Type, String) all dependencies defined via constructor parameters are automatically resolved. (The ServiceContainer automatically chooses the constructor with the max number of arguments. The method SelectConstructor(Type) can be overridden in derived classes if a different strategy for choosing a constructor should be applied.)
  • Property Injection: The method ResolveProperties(Object) can be called to inject dependencies into properties of a given instance. Note that, property injection is not applied automatically. The method ResolveProperties(Object) needs to be called explicitly, if property injection is required.

Named Services: Services can be registered under a name (key). The name can be nullNothingnullptra null reference (Nothing in Visual Basic), which is interpreted by the container as the "default" instance of the service. A string of length 0 is considered to be different from nullNothingnullptra null reference (Nothing in Visual Basic). Any service where the key is not nullNothingnullptra null reference (Nothing in Visual Basic) is called a "named" service. When the method GetInstance(Type, String) or its overloads are called without a key or with a nullNothingnullptra null reference (Nothing in Visual Basic) key, the default (unnamed) instance is returned or nullNothingnullptra null reference (Nothing in Visual Basic) if there is no default instance. And when the method GetInstance(Type, String) or its overloads are called with a key that is not nullNothingnullptra null reference (Nothing in Visual Basic), the matching named instance is returned or nullNothingnullptra null reference (Nothing in Visual Basic) if there is no matching named instance (even if there is a default instance).

When registering multiple services of the same type and with the same name (key), the previous entries will be overwritten.

IEnumerable<TService>: The method GetAllInstances(Type) returns all named instances of a given type. The method GetInstance(Type) and its overloads behave the same as GetAllInstances(Type) when the specified type is an IEnumerable<(Of <(<'T>)>)>. For example, GetInstance(typeof(IEnumerable<IServiceXyz>)) in C# returns all named instances of type IServiceXyz.

Func<TService>: The method GetInstance(Type, String) and its overloads can also be used to get a delegate method that resolves an instance of type T . The type parameter must be a Func<(Of <(<'TResult>)>)>. For example, GetInstance(typeof(Func<IServiceXyz>)) in C# returns a delegate that can be used to get an instance of type IServiceXyz.

Cyclic Service References: When services are registered by type (e.g. using Register(Type, String, Type)) the service container will automatically create an instance when needed and inject the necessary parameter in the constructor. Services that are registered by type must not have cyclic dependencies, for example, where service A needs service B in its constructor and B needs A in its constructor). Cyclic dependencies must be broken, for example, by changing the constructor of B to expect a Func of A (e.g. Func<A> in C# instead of an instance of A).

Registered Default Services: The service container itself is registered in the container by default and can be retrieved using, for example GetInstance<ServiceContainer>() (in C#).

Compatibility: If required, this class can be replaced by a more advanced IoC container implementing the IServiceLocator interface. For more information: See Microsoft patterns & practices - Common Service Locator Library.

Thread-Safety: The ServiceContainer is thread-safe and can be accessed from multiple threads simultaneously.

Inheritance Hierarchy

System..::..Object
  DigitalRune.ServiceLocation..::..ServiceContainer

See Also