Carubbi.ServiceLocator
2.0.0
dotnet add package Carubbi.ServiceLocator --version 2.0.0
NuGet\Install-Package Carubbi.ServiceLocator -Version 2.0.0
<PackageReference Include="Carubbi.ServiceLocator" Version="2.0.0" />
<PackageVersion Include="Carubbi.ServiceLocator" Version="2.0.0" />
<PackageReference Include="Carubbi.ServiceLocator" />
paket add Carubbi.ServiceLocator --version 2.0.0
#r "nuget: Carubbi.ServiceLocator, 2.0.0"
#:package Carubbi.ServiceLocator@2.0.0
#addin nuget:?package=Carubbi.ServiceLocator&version=2.0.0
#tool nuget:?package=Carubbi.ServiceLocator&version=2.0.0
Carubbi.ServiceLocator
A simple helper that implements the Service Locator pattern, resolving concrete implementations for interfaces or well-known keys through Microsoft.Extensions.Configuration.
Features
- Resolve interfaces to concrete types configured in any
IConfigurationsource (JSON, environment variables, in-memory, etc.) without a compile-time reference to the implementing project. - Resolve by friendly type name (including closed generics such as
"IRepository[Customer]") or by arbitrary configuration key. - Constructor injection of parameters through reflection.
- Singleton resolution through a public static
GetInstance()factory method. - Plugin discovery: scan a directory for assemblies exposing classes that implement both
IPluginand a contractT. - Strict error handling: missing sections, missing keys and unloadable types throw descriptive exceptions instead of failing silently.
Installation
dotnet add package Carubbi.ServiceLocator
Requires .NET 10.0 or later.
Usage
Given an appsettings.json:
{
"Implementations": {
"IMessageService": "MyApp.Services.EmailMessageService, MyApp",
"IRepository[int]": "MyApp.Data.IntRepository, MyApp"
},
"AppSettings": {
"ConnectionString": "Server=.;Database=MyDb;Trusted_Connection=True;"
}
}
Resolve implementations:
using Microsoft.Extensions.Configuration;
using Carubbi.ServiceLocator;
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var resolver = new ImplementationResolver(configuration);
// By interface type name
IMessageService service = resolver.Resolve<IMessageService>();
// By explicit key with constructor parameters
var repository = resolver.Resolve<IRepository<int>>("IRepository[int]");
// Singleton via a public static GetInstance() factory method
var singleton = resolver.ResolveSingleton<IMessageService>();
Read key/value settings from a section:
var settings = new AppSettings(configuration, "AppSettings");
string connectionString = settings["ConnectionString"];
Discover plugins:
List<IGreeter> plugins = PluginLocator.GetPlugins<IGreeter>("path/to/plugins");
Note: type names must be resolvable by
Type.GetType. Prefer assembly-qualified names (Namespace.Class, AssemblyName) and make sure the implementing assembly is available to the application.
Building and testing locally
Prerequisites:
- .NET SDK 10.0 or later
dotnet build -c Release
dotnet test --solution Carubbi.ServiceLocator.sln
The unit tests run entirely in memory; no Docker or external infrastructure is required.
Releases
Releases are published to nuget.org automatically by the publish workflow whenever a tag matching v* is pushed:
git tag v2.0.0
git push origin v2.0.0
License
Distributed under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.11)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Carubbi.ServiceLocator:
| Package | Downloads |
|---|---|
|
Carubbi.Mainframe
Wrapper de Emulador de Terminal Mainframe Microfocus Rumba |
|
|
Carubbi.Mailer.Interfaces
Contracts and dependency-injection helpers for sending and receiving email through pluggable providers such as SMTP/POP3, Exchange Web Services or Outlook interop. |
|
|
Carubbi.CaptchaBreaker
Wrapper do Tesseract e do Captcha Breaker para quebra de captchas |
GitHub repositories
This package is not used by any popular GitHub repositories.