PlatformTools.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Autofac;
  2. using InABox.Core;
  3. namespace InABox.Avalonia.Platform;
  4. public static class PlatformTools
  5. {
  6. private static ContainerBuilder _builder = new ContainerBuilder();
  7. private static IContainer? _container;
  8. private static IDeviceId? _deviceId;
  9. public static IDeviceId DeviceId
  10. {
  11. get
  12. {
  13. _deviceId ??= Resolve<IDeviceId,DefaultDeviceId>();
  14. return _deviceId;
  15. }
  16. }
  17. private static IAppVersion? _appVersion;
  18. public static IAppVersion AppVersion
  19. {
  20. get
  21. {
  22. _appVersion ??= Resolve<IAppVersion, DefaultAppVersion>();
  23. return _appVersion;
  24. }
  25. }
  26. private static IImageTools? _imageTools;
  27. public static IImageTools ImageTools
  28. {
  29. get
  30. {
  31. _imageTools ??= Resolve<IImageTools, DefaultImageTools>();
  32. return _imageTools;
  33. }
  34. }
  35. private static TInterface Resolve<TInterface, TDefault>() where TInterface : notnull, ILoggable where TDefault : TInterface, new()
  36. {
  37. _container ??= _builder.Build();
  38. var result = _container.IsRegistered<TInterface>()
  39. ?_container.Resolve<TInterface>()
  40. : new TDefault();
  41. result.Logger = Logger.Main;
  42. return result;
  43. }
  44. public static void Register<TInterface, TImplementation>()
  45. where TImplementation : TInterface
  46. where TInterface : notnull
  47. {
  48. _builder.RegisterType<TImplementation>().As<TInterface>();
  49. }
  50. }