Table of Contents

ServiceLocator Class

Definition

Namespace
Savvyio.Dispatchers
Assemblies
Savvyio.Core.dll
Source
src/Savvyio.Core/Dispatchers/ServiceLocator.cs

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

Name Description
ServiceLocator(Func<Type, IEnumerable<object>>)

Initializes a new instance of the ServiceLocator class.

Methods

Name Description
GetServices(Type)

Get an enumeration of services of type serviceType.

See Also