ServiceContainer Class |
Namespace: DigitalRune.ServiceLocation
The ServiceContainer type exposes the following members.
Name | Description | |
---|---|---|
ServiceContainer |
Initializes a new instance of the ServiceContainer class.
|
Name | Description | |
---|---|---|
Clear |
Resets the container and removes all locally registered service types.
| |
CreateChildContainer |
Creates a new child container.
| |
CreateInstance |
Creates an instance the given type and satisfies the constructor dependencies.
| |
Dispose |
Releases all resources used by an instance of the ServiceContainer class.
| |
Dispose(Boolean) |
Releases the unmanaged resources used by an instance of the
ServiceContainer class and optionally releases the managed resources.
| |
Equals | (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetAllInstances(Type) |
Gets all named instances of the given service type currently registered in the
container.
| |
GetAllInstancesTService |
Gets all named instances of the given service type currently registered in the
container.
| |
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) | |
GetInstance(Type) |
Get an instance of the given service type.
| |
GetInstance(Type, String) |
Gets a named instance of the given service type.
| |
GetInstanceTService |
Gets an instance of the given service type.
| |
GetInstanceTService(String) |
Gets a named instance of the given service type.
| |
GetService |
Gets the service instance of the specified type.
| |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
OnCreateChildContainer |
Called when a new child container needs to be created.
| |
OnCreateInstance |
Creates an instance of the type with the specified constructor arguments.
| |
Register(Type, String, FuncServiceContainer, Object) |
Registers a services using a custom factory method.
| |
Register(Type, String, Object) |
Registers the specified service instance.
| |
Register(Type, String, Type) |
Registers the specified service type.
| |
Register(Type, String, FuncServiceContainer, Object, CreationPolicy) |
Registers a services using a custom factory method and a certain creation policy.
| |
Register(Type, String, Type, CreationPolicy) |
Registers the specified service type using a certain creation policy.
| |
Register(Type, String, FuncServiceContainer, Object, CreationPolicy, DisposalPolicy) |
Registers a service using a custom factory method and certain creation and disposal
policies.
| |
Register(Type, String, Type, CreationPolicy, DisposalPolicy) |
Registers the specified service type using a certain creation and disposal policy.
| |
ResolveProperties |
Tries to resolve all property dependencies of the given instance.
| |
SelectConstructor |
Selects the constructor to be used for activating the given type.
| |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
Unregister(Type) |
Unregisters all services of the given service type.
| |
Unregister(Type, String) |
Unregisters the service with the specified name.
|
Name | Description | |
---|---|---|
IsDisposed |
Gets a value indicating whether this instance has been disposed of.
|
Name | Description | |
---|---|---|
IEnumerableGetEnumerator |
Returns an enumerator that iterates through all registered services.
|
The ServiceContainer supports basic dependency injection:
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.