SwiftLocator 1.0.2

dotnet add package SwiftLocator --version 1.0.2
NuGet\Install-Package SwiftLocator -Version 1.0.2
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="SwiftLocator" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SwiftLocator --version 1.0.2
#r "nuget: SwiftLocator, 1.0.2"
#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 SwiftLocator as a Cake Addin
#addin nuget:?package=SwiftLocator&version=1.0.2

// Install SwiftLocator as a Cake Tool
#tool nuget:?package=SwiftLocator&version=1.0.2

Logo SwiftLocator

Build Status NuGet version (SwiftLocator)

Description

SwiftLocator is a simple .NET 5 library that implements:

  • Transient, Scoped, Singleton service scopes.
  • Service Locator.
  • Optional Dependency Injection for the services.

How it works

There is a basic static class ServiceLocator that has registration and getting logic for transient, scoped, singleton service types. Registrator interface that ServiceLocator provides has many overloads, you can:

  • Pass instance
  • Use factory
  • Pass generic class type that will be used to construct new instances with automated dependency injection.
  • Pass generic class with generic interface/abstract type that will always be a real class representative while getting the service.

Transient doesn't support instance registration since the instance wouldn't be transient anymore as it is always the same.

Dependency injection

Dependency injection is automated for generic types you pass. It will look up whether your generic class constructor is asking for any properties and will inject them. Adding types that are not registered to the constructor will generate an error.

Singleton services can receive singleton and transient dependencies. Transient services can receive singleton and transient dependencies. Scoped services can receive all dependencies.

Option to do if you don't want automated dependency injection

  • Use an empty constructor
  • Register service with an instance
  • Register service with a factory function.

Use example

Here only a few examples are covered cause the namings of the methods are all the same and using the method overloads of this library are very intuitive.

Codespace somewhere where you have a first program initialization (recommended way of usage).

// Register singleton services.
ServiceLocator.SingletonRegistrator.Register<ParentTestClass>();

// Register transient services.
ServiceLocator.TransientRegistrator
    .Register<SecondChildInjectClass>()
    .Register<ChildInjectClass>()
    .Register<ChildChildInjectClass>();

Codespace anywhere in the project.


// Get singleton service with automatically injected transient 
// and scoped instances that ParentTestClass class is asking for in contstructor.
var singletonService = ServiceLocator.GetSingleton<ParentTestClass>();

// Get transient service with automatically injected transient 
// and scoped instances that ParentTestClass class is asking for in contstructor.
var singletonService = ServiceLocator.GetTransient<SecondChildInjectClass>();

Scoped example You can execute you're scope registration whenever or wherever you see fit.

// This key will act as a key to access the scope.
// With it, scoped service is the same as a singleton service, but it's tied to a key which allows singleton services 
// to act like scoped services if keys and registration are properly used.
const string scopeKey = "my random scope key";

ServiceLocator.GetScopedRegistrator(scopeKey)
    .Register<ParentTestClass>()
    .Register<SecondChildInjectClass>()
    .Register<ChildInjectClass>()
    .Register<ChildChildInjectClass>();

var scopedService = ServiceLocator.GetScoped<ParentTestClass>(scopeKey);

Urls

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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 Framework net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.8

    • No dependencies.
  • net5.0

    • No dependencies.

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.2 148 5/23/2023
1.0.1 113 5/18/2023
1.0.0 108 5/16/2023

Major dependency injection bug fix.