SwiftLocator 1.0.2
dotnet add package SwiftLocator --version 1.0.2
NuGet\Install-Package SwiftLocator -Version 1.0.2
<PackageReference Include="SwiftLocator" Version="1.0.2" />
paket add SwiftLocator --version 1.0.2
#r "nuget: SwiftLocator, 1.0.2"
// 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
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 | Versions 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. |
-
.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.
Major dependency injection bug fix.