You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.8 KiB

2 years ago
  1. #if Middleware
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Apewer.Web
  6. {
  7. /// <summary>API 服务描述。</summary>
  8. public sealed class ApiServiceDescriptor : IToJson
  9. {
  10. /// <summary>订阅器的生命周期。</summary>
  11. public ApiServiceLifetime Lifetime { get; private set; }
  12. /// <summary>服务的类型。</summary>
  13. public Type ServiceType { get; private set; }
  14. /// <summary>实现服务的类型。</summary>
  15. public Type ImplementationType { get; private set; }
  16. public object ImplementationInstance
  17. {
  18. get;
  19. }
  20. public Func<IServiceProvider, object> ImplementationFactory
  21. {
  22. get;
  23. }
  24. /// <summary>创建订阅器的实例。</summary>
  25. /// <param name="invoker">API 调用器。</param>
  26. /// <param name="lifetime">订阅器的生命周期。</param>
  27. /// <param name="service">服务的类型。</param>
  28. /// <param name="implementation">实现服务的类型。</param>
  29. /// <exception cref="ArgumentNullException" />
  30. /// <exception cref="ArgumentException" />
  31. internal ApiServiceDescriptor(ApiServiceLifetime lifetime, Type service, Type implementation)
  32. {
  33. if (invoker == null) throw new ArgumentNullException(nameof(invoker));
  34. if (service == null) throw new ArgumentNullException(nameof(service));
  35. if (implementation == null) throw new ArgumentNullException(nameof(implementation));
  36. if (!service.IsAssignableFrom(implementation)) throw new ArgumentException($"类型 {implementation.Name} 未实现服务。");
  37. if (!implementation.IsClass) throw new ArgumentException($"实现服务的类型 {implementation.Name} 不是引用类型。");
  38. if (implementation.IsAbstract) throw new ArgumentException($"实现服务的类型 {implementation.Name} 是抽象类型,无法实例化。");
  39. Lifetime = lifetime;
  40. ServiceType = service;
  41. ImplementationType = implementation;
  42. }
  43. /// <summary>创建订阅器的实例。</summary>
  44. /// <param name="invoker">API 调用器。</param>
  45. /// <param name="lifetime">订阅器的生命周期。</param>
  46. /// <param name="service">服务的类型。</param>
  47. /// <param name="implementation">实现服务的方法。</param>
  48. /// <exception cref="ArgumentNullException" />
  49. /// <exception cref="ArgumentException" />
  50. internal ApiServiceDescriptor(ApiServiceLifetime lifetime, Type service, Func<Type, implementation)
  51. {
  52. if (invoker == null) throw new ArgumentNullException(nameof(invoker));
  53. if (service == null) throw new ArgumentNullException(nameof(service));
  54. if (implementation == null) throw new ArgumentNullException(nameof(implementation));
  55. if (!service.IsAssignableFrom(implementation)) throw new ArgumentException($"类型 {implementation.Name} 未实现服务。");
  56. if (!implementation.IsClass) throw new ArgumentException($"实现服务的类型 {implementation.Name} 不是引用类型。");
  57. if (implementation.IsAbstract) throw new ArgumentException($"实现服务的类型 {implementation.Name} 是抽象类型,无法实例化。");
  58. Invoker = invoker;
  59. Lifetime = lifetime;
  60. ServiceType = service;
  61. ImplementationType = implementation;
  62. }
  63. /// <summary>生成 JSON 实例。</summary>
  64. public Json ToJson()
  65. {
  66. var json = new Json();
  67. json["Lifetime"] = Lifetime.ToString();
  68. json["Service"] = ServiceType.FullName;
  69. json["Implementation"] = Implementation.FullName;
  70. return json;
  71. }
  72. }
  73. }
  74. #endif