C# Class CK.Core.SimpleServiceContainer

Service container (that is a IServiceProvider) subordinated to an optional base IServiceProvider that acts as a fallback if the service is not found at this level. Service creation may be deferred thanks to callback registration and an optional remove callback can be registered with each entry.
This container is registered as the service associated to IServiceProvider and ISimpleServiceContainer thanks to the overridable GetDirectService. This method may be overridden to return other built-in services: these services take precedence over the registered services.
Inheritance: ISimpleServiceContainer, IDisposable
ファイルを表示 Open project: Invenietis/ck-core Class Usage Examples

Public Methods

Method Description
Add ( Type serviceType, Func serviceInstance, Action onRemove ) : ISimpleServiceContainer

Registers a service associated to a callback (and an optional callback that will be called when the service will be removed). The serviceInstance is called as long as no service has been obtained (serviceInstance returns null). Once the actual service has been obtained, it is kept and serviceInstance is not called anymore.

Add ( Type serviceType, object serviceInstance, Action onRemove ) : ISimpleServiceContainer

Registers a service with its implementation (and an optional callback that will be called when the service will be removed).

AddDisabled ( Type serviceType ) : ISimpleServiceContainer

Disables a service: null will always be returned by this IServiceProvider.GetService regardless of any fallbacks from BaseProvider. Direct services returned by GetDirectService can not be disabled.

This is not the same as calling Add(Type,Func{Object},Action{Object}) with a null instance. A null instance for a service (a callback that always returns null) is nearly the same as calling Remove: any fallbacks (to a base IServiceProvider for example) can occur. This is stronger since this must prevent fallbacks.

Clear ( ) : ISimpleServiceContainer

Unregisters all the services. Any "on remove" actions are executed.

Dispose ( ) : void

Disposing calls Clear to unregister all services. Any "on remove" actions are executed.

GetService ( Type serviceType ) : object

Implements IServiceProvider.GetService.

Remove ( Type serviceType ) : ISimpleServiceContainer

Unregisters a service. Can be called even if the service does not exist. The service is first removed and then the OnRemove associated action is called if it exists.

SimpleServiceContainer ( ) : System

Initializes a new SimpleServiceContainer.

SimpleServiceContainer ( IServiceProvider baseProvider ) : System

Initializes a new SimpleServiceContainer with a BaseProvider.

Protected Methods

Method Description
Dispose ( bool disposing ) : void

When disposing is true, calls Clear to unregister all services. Any "on remove" actions are executed. Whether Dispose() has been called.

GetDirectService ( Type serviceType ) : object

Must return built-in services if any. These services take precedence over any registered services. This base implementation returns this object for IServiceProvider and ISimpleServiceContainer.

Private Methods

Method Description
DoAdd ( Type s, ServiceEntry e ) : void

Correct ArgumentException throw by a Dictionary when adding an existing key.

Method Details

Add() public method

Registers a service associated to a callback (and an optional callback that will be called when the service will be removed). The serviceInstance is called as long as no service has been obtained (serviceInstance returns null). Once the actual service has been obtained, it is kept and serviceInstance is not called anymore.
public Add ( Type serviceType, Func serviceInstance, Action onRemove ) : ISimpleServiceContainer
serviceType System.Type Service type to register. It must not already exist in this container otherwise an exception is thrown.
serviceInstance Func Delegate to call when needed. Can not be null.
onRemove Action Optional action that will be called whenever , or /// is called and a service as been successfully obtained.
return ISimpleServiceContainer

Add() public method

Registers a service with its implementation (and an optional callback that will be called when the service will be removed).
public Add ( Type serviceType, object serviceInstance, Action onRemove ) : ISimpleServiceContainer
serviceType System.Type Service type to register. It must not already exist in this container otherwise an exception is thrown.
serviceInstance object Implementation of the service. Can not be null.
onRemove Action Optional action that will be called whenever , or .
return ISimpleServiceContainer

AddDisabled() public method

Disables a service: null will always be returned by this IServiceProvider.GetService regardless of any fallbacks from BaseProvider. Direct services returned by GetDirectService can not be disabled.
This is not the same as calling Add(Type,Func{Object},Action{Object}) with a null instance. A null instance for a service (a callback that always returns null) is nearly the same as calling Remove: any fallbacks (to a base IServiceProvider for example) can occur. This is stronger since this must prevent fallbacks.
public AddDisabled ( Type serviceType ) : ISimpleServiceContainer
serviceType System.Type Service type to disable. It must not already exist in this container otherwise an exception is thrown.
return ISimpleServiceContainer

Clear() public method

Unregisters all the services. Any "on remove" actions are executed.
public Clear ( ) : ISimpleServiceContainer
return ISimpleServiceContainer

Dispose() public method

Disposing calls Clear to unregister all services. Any "on remove" actions are executed.
public Dispose ( ) : void
return void

Dispose() protected method

When disposing is true, calls Clear to unregister all services. Any "on remove" actions are executed. Whether Dispose() has been called.
protected Dispose ( bool disposing ) : void
disposing bool
return void

GetDirectService() protected method

Must return built-in services if any. These services take precedence over any registered services. This base implementation returns this object for IServiceProvider and ISimpleServiceContainer.
protected GetDirectService ( Type serviceType ) : object
serviceType System.Type Type of the service to obtain.
return object

GetService() public method

Implements IServiceProvider.GetService.
public GetService ( Type serviceType ) : object
serviceType System.Type Type of the service to obtain.
return object

Remove() public method

Unregisters a service. Can be called even if the service does not exist. The service is first removed and then the OnRemove associated action is called if it exists.
public Remove ( Type serviceType ) : ISimpleServiceContainer
serviceType System.Type Service type to unregister.
return ISimpleServiceContainer

SimpleServiceContainer() public method

Initializes a new SimpleServiceContainer.
public SimpleServiceContainer ( ) : System
return System

SimpleServiceContainer() public method

Initializes a new SimpleServiceContainer with a BaseProvider.
public SimpleServiceContainer ( IServiceProvider baseProvider ) : System
baseProvider IServiceProvider Base provider.
return System