PlatformTools.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Autofac;
  2. using InABox.Avalonia.Platform;
  3. using InABox.Core;
  4. namespace InABox.Avalonia.Platform;
  5. public static class PlatformTools
  6. {
  7. private static ContainerBuilder _builder = new ContainerBuilder();
  8. private static IContainer? _container;
  9. private static IDeviceId? _deviceId;
  10. public static IDeviceId DeviceId
  11. {
  12. get
  13. {
  14. _deviceId ??= Resolve<IDeviceId,DefaultDeviceId>();
  15. return _deviceId;
  16. }
  17. }
  18. private static IAppVersion? _appVersion;
  19. public static IAppVersion AppVersion
  20. {
  21. get
  22. {
  23. _appVersion ??= Resolve<IAppVersion, DefaultAppVersion>();
  24. return _appVersion;
  25. }
  26. }
  27. private static IImageTools? _imageTools;
  28. public static IImageTools ImageTools
  29. {
  30. get
  31. {
  32. _imageTools ??= Resolve<IImageTools, DefaultImageTools>();
  33. return _imageTools;
  34. }
  35. }
  36. private static IPdfRenderer? _pdfRenderer;
  37. public static IPdfRenderer PdfRenderer
  38. {
  39. get
  40. {
  41. _pdfRenderer ??= Resolve<IPdfRenderer, DefaultPdfRenderer>();
  42. return _pdfRenderer;
  43. }
  44. }
  45. private static IBluetooth? _bluetooth;
  46. public static IBluetooth Bluetooth
  47. {
  48. get
  49. {
  50. _bluetooth ??= Resolve<IBluetooth, DefaultBluetooth>();
  51. return _bluetooth;
  52. }
  53. }
  54. private static TInterface Resolve<TInterface, TDefault>() where TInterface : notnull, ILoggable where TDefault : TInterface, new()
  55. {
  56. _container ??= _builder.Build();
  57. var result = _container.IsRegistered<TInterface>()
  58. ?_container.Resolve<TInterface>()
  59. : new TDefault();
  60. result.Logger = Logger.Main;
  61. return result;
  62. }
  63. public static void Register<TInterface, TImplementation>()
  64. where TImplementation : TInterface
  65. where TInterface : notnull
  66. {
  67. _builder.RegisterType<TImplementation>().As<TInterface>();
  68. }
  69. }