Ckode.ServiceLocator 2.3.0

dotnet add package Ckode.ServiceLocator --version 2.3.0
NuGet\Install-Package Ckode.ServiceLocator -Version 2.3.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ckode.ServiceLocator" Version="2.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ckode.ServiceLocator --version 2.3.0
#r "nuget: Ckode.ServiceLocator, 2.3.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Ckode.ServiceLocator as a Cake Addin
#addin nuget:?package=Ckode.ServiceLocator&version=2.3.0

// Install Ckode.ServiceLocator as a Cake Tool
#tool nuget:?package=Ckode.ServiceLocator&version=2.3.0

Ckode.ServiceLocator

Ckode.ServiceLocator is a simple natively implemented service locator for simplifying dependency injection.

Installation:

I recommend using the NuGet package: https://www.nuget.org/packages/Ckode.ServiceLocator/ however you can also simply clone the repository and use the pre-compiled binaries or compile the project yourself. As the project is licensed under MIT you're free to use it for pretty much anything you want.

Examples:

Create a single instance:

ISomeInterface instance = ServiceLocator.CreateInstance<ISomeInterface>(); // Requires that only a single class implements ISomeInterface

Create multiple instances:

IEnumerable<ISomeInterface> instances = ServiceLocator.CreateInstances<ISomeInterface>(); // Works regardless of the number of implementations, as you get an IEnumerable of instances

Create instance based on predicate:

ISomeInterface instance = ServiceLocator.CreateInstance<ISomeInterface>(inst => inst.UseThisOne(someArgument)); // Requires that only a single class implements ISomeInterface AND fulfills the predicate

Fake dependency for e.g. unit tests:

public interface IDependency
{
    string Value{ get; }
}

public class ActualImplementation : IDependency
{
    // Some actual implementation
}

public class TextFormatter
{
    public string AppendValueFromDependency(string textToAppendTo)
    {
        var dependency = ServiceLocator.CreateInstance<IDependency>();
        return textToAppendTo + dependency.Value;
    }
}

public class TextFormattersTests
{
    private class MyFake : IDependency
    {
        public string Value => "World";
    }

    [Fact]
    public void TextFormatter_AppendValueFromDependency_GeneratesProperString()
    {
        // Arrange
        ServiceLocator.Bind<IDependency, MyFake>(); // Forces TextFormatter to use MyFake instead of whatever the ActualImplementation was doing
        var formatter = new TextFormatter();
        
        // Act
        var value = formatter.AppendValueFromDependency("Hello ");
        
        // Assert
        Assert.Equal("Hello world", value);
        
        // Cleanup
        ServiceLocator.Unbind<IDependency>(); // Optionally unbind IDependency, if you don't want it to be MyFake in other tests as the binding is static
    }
}

Create instance based on a key:

public enum VehicleType
{
    Car,
    Bike,
    Plane,
    Boat
}

public interface IVehicle: ILocatable<VehicleType>
{
}

public class Car: IVehicle
{
    public VehicleType LocatorKey => VehicleType.Car;
}

var locator = new ServiceLocator<VehicleType, IVehicle();
IVehicle car = locator.CreateInstance(VehicleType.Car); // Requires that only a single class has LocatorKey == VehicleType.Car

Create multiple instances with keys:

public enum VehicleType
{
    Car,
    Bike,
    Plane,
    Boat
}

public interface IVehicle: ILocatable<VehicleType>
{
}

public class Car: IVehicle
{
    public VehicleType LocatorKey => VehicleType.Car;
}

var locator = new ServiceLocator<VehicleType, IVehicle>();
IEnumerable<IVehicle> vehicles = locator.CreateInstances(); // Still requires that only a single class has each LocatorKey, but does work fine despite some keys not being implemented yet.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.3.0 4,912 1/23/2024
2.2.0 9,536 3/10/2021
2.1.1 372 3/7/2021
1.1.1 792 1/27/2019
1.1.0 975 3/20/2018
1.0.1 935 3/11/2018
1.0.0 945 3/11/2018