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

#if Middleware
using System;
using System.Collections.Generic;
using System.Text;
namespace Apewer.Web
{
/// <summary>API 服务描述。</summary>
public sealed class ApiServiceDescriptor : IToJson
{
/// <summary>订阅器的生命周期。</summary>
public ApiServiceLifetime Lifetime { get; private set; }
/// <summary>服务的类型。</summary>
public Type ServiceType { get; private set; }
/// <summary>实现服务的类型。</summary>
public Type ImplementationType { get; private set; }
public object ImplementationInstance
{
get;
}
public Func<IServiceProvider, object> ImplementationFactory
{
get;
}
/// <summary>创建订阅器的实例。</summary>
/// <param name="invoker">API 调用器。</param>
/// <param name="lifetime">订阅器的生命周期。</param>
/// <param name="service">服务的类型。</param>
/// <param name="implementation">实现服务的类型。</param>
/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
internal ApiServiceDescriptor(ApiServiceLifetime lifetime, Type service, Type implementation)
{
if (invoker == null) throw new ArgumentNullException(nameof(invoker));
if (service == null) throw new ArgumentNullException(nameof(service));
if (implementation == null) throw new ArgumentNullException(nameof(implementation));
if (!service.IsAssignableFrom(implementation)) throw new ArgumentException($"类型 {implementation.Name} 未实现服务。");
if (!implementation.IsClass) throw new ArgumentException($"实现服务的类型 {implementation.Name} 不是引用类型。");
if (implementation.IsAbstract) throw new ArgumentException($"实现服务的类型 {implementation.Name} 是抽象类型,无法实例化。");
Lifetime = lifetime;
ServiceType = service;
ImplementationType = implementation;
}
/// <summary>创建订阅器的实例。</summary>
/// <param name="invoker">API 调用器。</param>
/// <param name="lifetime">订阅器的生命周期。</param>
/// <param name="service">服务的类型。</param>
/// <param name="implementation">实现服务的方法。</param>
/// <exception cref="ArgumentNullException" />
/// <exception cref="ArgumentException" />
internal ApiServiceDescriptor(ApiServiceLifetime lifetime, Type service, Func<Type, implementation)
{
if (invoker == null) throw new ArgumentNullException(nameof(invoker));
if (service == null) throw new ArgumentNullException(nameof(service));
if (implementation == null) throw new ArgumentNullException(nameof(implementation));
if (!service.IsAssignableFrom(implementation)) throw new ArgumentException($"类型 {implementation.Name} 未实现服务。");
if (!implementation.IsClass) throw new ArgumentException($"实现服务的类型 {implementation.Name} 不是引用类型。");
if (implementation.IsAbstract) throw new ArgumentException($"实现服务的类型 {implementation.Name} 是抽象类型,无法实例化。");
Invoker = invoker;
Lifetime = lifetime;
ServiceType = service;
ImplementationType = implementation;
}
/// <summary>生成 JSON 实例。</summary>
public Json ToJson()
{
var json = new Json();
json["Lifetime"] = Lifetime.ToString();
json["Service"] = ServiceType.FullName;
json["Implementation"] = Implementation.FullName;
return json;
}
}
}
#endif