ServiceProvider.cs 614 B

1234567891011121314151617181920212223
  1. using System;
  2. using System.Collections.Generic;
  3. namespace FF8
  4. {
  5. public sealed class ServiceProvider : IServices
  6. {
  7. private readonly Dictionary<Object, Object> _services = new Dictionary<Object, Object>();
  8. public void Register<T>(ServiceId<T> id, T service)
  9. {
  10. _services.Add(id, service);
  11. }
  12. public T Service<T>(ServiceId<T> id)
  13. {
  14. if (_services.TryGetValue(id, out var value))
  15. return (T)value;
  16. throw new ArgumentException($"Service {typeof(T).FullName} isn't registered.", nameof(id));
  17. }
  18. }
  19. }