vb2ae.ServiceLocator.MSDependencyInjection 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package vb2ae.ServiceLocator.MSDependencyInjection --version 1.0.5                
NuGet\Install-Package vb2ae.ServiceLocator.MSDependencyInjection -Version 1.0.5                
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="vb2ae.ServiceLocator.MSDependencyInjection" Version="1.0.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add vb2ae.ServiceLocator.MSDependencyInjection --version 1.0.5                
#r "nuget: vb2ae.ServiceLocator.MSDependencyInjection, 1.0.5"                
#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 vb2ae.ServiceLocator.MSDependencyInjection as a Cake Addin
#addin nuget:?package=vb2ae.ServiceLocator.MSDependencyInjection&version=1.0.5

// Install vb2ae.ServiceLocator.MSDependencyInjection as a Cake Tool
#tool nuget:?package=vb2ae.ServiceLocator.MSDependencyInjection&version=1.0.5                

vb2ae.ServiceLocator.MSDependencyInjection

.NET CodeFactor

CodeQL

Dependabot Updates

badge

NuGet Package

What is the common service locator?

The Common Service Locator (CSL) library serves as a shared interface for service location, aimed at both application and framework developers. By providing an abstraction layer over IoC (Inversion of Control) containers and service locators, the CSL library allows applications to access these capabilities indirectly, without the need for hard references. This design ensures that third-party applications and frameworks can leverage IoC and service location features without being locked into a specific implementation. Ultimately, the goal is to promote flexibility and interoperability across various development ecosystems.

What is vb2ae.ServiceLocator.MSDependencyInjection?

vb2ae.ServiceLocator.MSDependencyInjection is an implementation of the Common Service Locator (CSL) for the Microsoft.Extensions.DependencyInjection library. Currently, it uses version 9 of Microsoft.Extensions.DependencyInjection, enabling it to support all Keyed services effectively.

How to register your services

The ConfigureServices method is where we are registering the Objects for dependency injection

    private IServiceProvider Build()
    {
        if (_built)
            throw new InvalidOperationException("Build can only be called once.");
        _built = true;

        _defaultBuilder.ConfigureServices((context, services) =>
        {
            services.AddSingleton<IService, ServiceImpl>();
            services.AddTransient<ICar, Ford>();
            services.AddTransient<ICar, Toyota>();
            services.AddTransient<ICar, Chevy>();
            services.AddKeyedTransient<IPet, Dog>("Dog");
            services.AddKeyedTransient<IPet, Cat>("Cat");
        });

        _services = _defaultBuilder.Build().Services;
        CommonServiceLocator.ServiceLocator.SetLocatorProvider(() => new vb2ae.ServiceLocator.MSDependencyInjection.MSDependencyInjectionServiceLocator(_services));
        return _services;
    }

The line of code below registers vb2ae.ServiceLocator.MSDependencyInjection with the CommonServiceLocator

CommonServiceLocator.ServiceLocator.SetLocatorProvider(() => new vb2ae.ServiceLocator.MSDependencyInjection.MSDependencyInjectionServiceLocator(_services));
        return _services;

How to Get classes stored in Dependency Injection with the CommonServiceLocator

GetService

        var serviceType = typeof(IService);
        var result = CommonServiceLocator.ServiceLocator.Current.GetService(serviceType);

This returns a ServiceImpl

GetService< T >

        var result = CommonServiceLocator.ServiceLocator.Current.GetInstance<IService>();

This returns a ServiceImpl

GetInstance(serviceType, key)

        var serviceType = typeof(IPet);
        var key = "Cat";
        var result = CommonServiceLocator.ServiceLocator.Current.GetInstance(serviceType, key);

This returns an instance of Cat

GetInstance< T >(key)

        var key = "Dog";
        var result = CommonServiceLocator.ServiceLocator.Current.GetInstance<IPet>(key);

This returns an instance of Dog

GetInstance(serviceType)

        var serviceType = typeof(IService);
        var result = CommonServiceLocator.ServiceLocator.Current.GetInstance(serviceType);
        

This returns a ServiceImpl

GetInstance< T >

        var result = CommonServiceLocator.ServiceLocator.Current.GetInstance<IService>();

This returns a ServiceImpl

GetAllInstances(ServiceType)

        var result = CommonServiceLocator.ServiceLocator.Current.GetAllInstances(typeof(IPet));

Will return an IEnumerable<object> that contains a Cat and Dog

GetAllInstances< T >

       var result = CommonServiceLocator.ServiceLocator.Current.GetAllInstances<ICar>()

Will return an IEnumerable<ICar> that contains a Ford, Chevy and Toyota

Notes

Passing a null in as the key will return the first of the Type registered

        var serviceType = typeof(IPet);
        var result = CommonServiceLocator.ServiceLocator.Current.GetInstance(serviceType, null);

Will return the First IPet registered for DependencyInjection in this case an instance of the Dog class

All examples are based on what is registered in the How to register your services section

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.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
1.0.7 89 12/22/2024
1.0.5 85 12/14/2024
0.9.0 97 12/6/2024
0.7.0 93 11/24/2024
0.5.0 96 10/30/2024