Skip to content

Conversation

@mokarchi
Copy link
Contributor

@mokarchi mokarchi commented Oct 3, 2025

Overview

This PR adds support for keyed service registration in the Scan method, enabling compatibility with .NET 8's keyed dependency injection feature. This allows users to specify service keys during scanning, aligning with the AddKeyedSingleton/AddKeyedScoped/AddKeyedTransient pattern introduced in .NET 8.

Problem

Previously, there was no way to register services with keys when using Scrutor's scanning capabilities. Users who wanted to leverage .NET 8's keyed DI feature had to manually register services, losing the benefits of convention-based scanning.

Solution

Added two new methods to ILifetimeSelector:

  • WithServiceKey(object serviceKey) - Registers all scanned services with a fixed service key
  • WithServiceKey(Func<Type, object?> selector) - Registers services with dynamic keys based on a selector function

Usage Examples

Fixed Service Key

Register all scanned services with the same key:

services.Scan(scan => scan
    .FromAssemblyOf<MyService>()
    .AddClasses()
    .AsImplementedInterfaces()
    .WithServiceKey("my-key")
    .WithTransientLifetime());

// Resolve using the key
var service = provider.GetRequiredKeyedService<IMyService>("my-key");

Dynamic Service Key

Use a selector function to assign different keys based on the implementation type:

services.Scan(scan => scan
    .FromAssemblyOf<IRepository>()
    .AddClasses(c => c.AssignableTo<IRepository>())
    .AsImplementedInterfaces()
    .WithServiceKey(type => type.Name.Replace("Repository", ""))
    .WithScopedLifetime());

// Resolve specific repositories by key
var customerRepo = provider.GetRequiredKeyedService<IRepository>("Customer");
var orderRepo = provider.GetRequiredKeyedService<IRepository>("Order");

Introduced service key registration in the `ILifetimeSelector` interface with two new methods: `WithServiceKey(object serviceKey)` and `WithServiceKey(Func<Type, object?> selector)`. These enable registering services with fixed or dynamically determined service keys.

Updated the `LifetimeSelector` class to handle service key functionality, including adding the `ServiceKeySelectorFn` property, modifying the `Populate` method to support keyed `ServiceDescriptor` creation, and adding helper methods for service key resolution.

Enhanced the `ServiceTypeSelector` class to propagate service key selectors to `LifetimeSelector` instances.

Added unit tests in `ScanningTests.cs` to validate service key registration and resolution, including tests for fixed keys, selector-based keys, and keyed service resolution.

Made minor formatting adjustments and ensured proper validation of the new functionality.
@mokarchi
Copy link
Contributor Author

mokarchi commented Oct 6, 2025

@khellang Could you review this, please?

@khellang khellang merged commit 2cdfc8d into khellang:master Nov 24, 2025
This was referenced Nov 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants