Khi có nhiều implementation của cùng một interface, DI truyền thống chỉ resolve được implementation cuối đăng ký.
- Keyed services cho phép đặt tên:
services.AddKeyedScoped<INotifier, EmailNotifier>("email"). - Inject bằng key:
[FromKeyedServices("email")] INotifier notifier. - Giải quyết: factory pattern phức tạp, service locator antipattern, nhập nhằng naming.
- Tính năng mới từ .NET 8; sạch hơn factory method.
- Dùng khi interface có nhiều implementation và context quyết định dùng cái nào.
When multiple implementations share an interface, traditional DI resolves only the last registered implementation.
- Keyed services allow naming registrations:
services.AddKeyedScoped<INotifier, EmailNotifier>("email"). - Inject by key:
[FromKeyedServices("email")] INotifier notifier. - This resolves complex factory patterns, the service locator antipattern, and naming ambiguity.
- A .NET 8+ feature that is cleaner than factory methods.
- Use when an interface has multiple implementations and context determines which is needed.