Class ServiceLocator
- Namespace
- Savvyio.Dispatchers
- Assembly
- Savvyio.Core.dll
Provides a default implementation of the IServiceLocator interface.
public class ServiceLocator : IServiceLocator
- Inheritance
-
ServiceLocator
- Implements
Examples
This example shows how to adapt an application service collection to Savvy I/O with a lightweight service locator.
using System;
using System.Collections.Generic;
using System.Linq;
using Savvyio.Dispatchers;
namespace ExampleApp;
public sealed class ServiceLocatorExample
{
public bool CanResolveDispatcher()
{
var services = new Dictionary<Type, object>
{
[typeof(ICheckoutDispatcher)] = new CheckoutDispatcher()
};
var locator = new ServiceLocator(serviceType =>
services.TryGetValue(serviceType, out var service)
? new[] { service }
: Array.Empty<object>());
return locator.GetServices(typeof(ICheckoutDispatcher)).Single() is CheckoutDispatcher;
}
}
public interface ICheckoutDispatcher
{
}
public sealed class CheckoutDispatcher : ICheckoutDispatcher
{
}
Constructors
ServiceLocator(Func<Type, IEnumerable<object>>)
Initializes a new instance of the ServiceLocator class.
public ServiceLocator(Func<Type, IEnumerable<object>> serviceFactory)
Parameters
serviceFactoryFunc<Type, IEnumerable<object>>The function delegate that provides the services.
Methods
GetServices(Type)
Get an enumeration of services of type serviceType.
public IEnumerable<object> GetServices(Type serviceType)
Parameters
serviceTypeTypeAn object that specifies the type of service object to get.
Returns
- IEnumerable<object>
An enumeration of services of type
serviceType.