Trung BìnhC# iconC#

Keyed Services (`[FromKeyedServices]`) trong .NET 8 giải quyết vấn đề gì?

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.

Xem toàn bộ C# cùng filter theo level & chủ đề con.

Mở danh sách C#