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.