Click or drag to resize
DigitalRuneServiceContainer Class
Implements a simple inversion of control (IoC) container.
Inheritance Hierarchy
SystemObject
  DigitalRune.ServiceLocationServiceContainer

Namespace: DigitalRune.ServiceLocation
Assembly: DigitalRune.ServiceLocation (in DigitalRune.ServiceLocation.dll) Version: 1.3.0.0 (1.3.0.13596)
Syntax
public class ServiceContainer : IEnumerable, 
	IServiceLocator, IServiceProvider, IDisposable

The ServiceContainer type exposes the following members.

Constructors
  NameDescription
Public methodServiceContainer
Initializes a new instance of the ServiceContainer class.
Top
Methods
  NameDescription
Public methodClear
Resets the container and removes all locally registered service types.
Public methodCreateChildContainer
Creates a new child container.
Public methodCreateInstance
Creates an instance the given type and satisfies the constructor dependencies.
Public methodDispose
Releases all resources used by an instance of the ServiceContainer class.
Protected methodDispose(Boolean)
Releases the unmanaged resources used by an instance of the ServiceContainer class and optionally releases the managed resources.
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetAllInstances(Type)
Gets all named instances of the given service type currently registered in the container.
Public methodGetAllInstancesTService
Gets all named instances of the given service type currently registered in the container.
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetInstance(Type)
Get an instance of the given service type.
Public methodGetInstance(Type, String)
Gets a named instance of the given service type.
Public methodGetInstanceTService
Gets an instance of the given service type.
Public methodGetInstanceTService(String)
Gets a named instance of the given service type.
Public methodGetService
Gets the service instance of the specified type.
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodOnCreateChildContainer
Called when a new child container needs to be created.
Protected methodOnCreateInstance
Creates an instance of the type with the specified constructor arguments.
Public methodRegister(Type, String, FuncServiceContainer, Object)
Registers a services using a custom factory method.
Public methodRegister(Type, String, Object)
Registers the specified service instance.
Public methodRegister(Type, String, Type)
Registers the specified service type.
Public methodRegister(Type, String, FuncServiceContainer, Object, CreationPolicy)
Registers a services using a custom factory method and a certain creation policy.
Public methodRegister(Type, String, Type, CreationPolicy)
Registers the specified service type using a certain creation policy.
Public methodRegister(Type, String, FuncServiceContainer, Object, CreationPolicy, DisposalPolicy)
Registers a service using a custom factory method and certain creation and disposal policies.
Public methodRegister(Type, String, Type, CreationPolicy, DisposalPolicy)
Registers the specified service type using a certain creation and disposal policy.
Public methodResolveProperties
Tries to resolve all property dependencies of the given instance.
Protected methodSelectConstructor
Selects the constructor to be used for activating the given type.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodUnregister(Type)
Unregisters all services of the given service type.
Public methodUnregister(Type, String)
Unregisters the service with the specified name.
Top
Properties
  NameDescription
Public propertyIsDisposed
Gets a value indicating whether this instance has been disposed of.
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implementationPrivate methodIEnumerableGetEnumerator
Returns an enumerator that iterates through all registered services.
Top
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 , which is interpreted by the container as the "default" instance of the service. A string of length 0 is considered to be different from . Any service where the key is not is called a "named" service. When the method GetInstance(Type, String) or its overloads are called without a key or with a key, the default (unnamed) instance is returned or if there is no default instance. And when the method GetInstance(Type, String) or its overloads are called with a key that is not , the matching named instance is returned or 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 IEnumerableT. 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 FuncTResult. 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.

See Also