Skip to content

Latest commit

 

History

History
82 lines (53 loc) · 3.24 KB

File metadata and controls

82 lines (53 loc) · 3.24 KB

ServiceHost Class

Index > ExpressionPowerTools.Core > ExpressionPowerTools.Core.Dependencies > ServiceHost

Host to obtain services.

public static class ServiceHost

Inheritance ObjectServiceHost

Examples

To register a type MyType that implements IMyType :

ServiceHost.Initialize(register => register.Register<IMyType, MyType>());
            

To register a singleton:

var singleton = new MyType();
            ServiceHost.Initialize(register => register.RegisterSingleton<IMyType>(singleton);
            

To register a generic type IGenericType<T> that is implemented by GenericType<T> :

ServiceHost.Initialize(register =>
                register.RegisterGeneric(typeof(IGenericType<>), typeof(GenericType<>)));
            

To retrieve a service:

var implementation = ServiceHost.GetService<IMyType>();
            

Retrieving a generic service (closed) with parameters:

var implementation = ServiceHost.GetService<IGenericType<string>>(5, 6);
            

Remarks

This is a very simple dependency injection container. It allows registration of type implementations, generic implementations, and singletons. It does not recursively resolve dependencies. Registration happens in the call toand can only be done once.is provided mainly for testing purposes.

Satellite assemblies can hook into the registration by implementing IDependentServiceRegistration . This is scanned and loaded before user overridees. Services can be lazy-loaded by using themethod.

The IServiceRegistration provided by initialization is chainable (each call returns itself).

Constructors

Ctor Description
static ServiceHost() Initializes static members of the ServiceHost class.

Methods

Method Description
Lazy<T> GetLazyService<T>(Object[] parameters) Gets a lazy loaded service.
T GetService<T>(Object[] parameters) Get the service implementation.
Void Initialize(Action<IServiceRegistration> registration) Initialize the container. Can only be done once unlessis called.
Void Reset() Reset to new services instance.
IList<Type> SafeGetTypes(Predicate<Type> filter) Safely enumerate types across loaded assemblies.

Generated Copyright Version
02/22/2021 21:59:57 (c) Copyright 2020 Jeremy Likness. 0.9.7-beta