ServiceCollection class
ServiceCollection 是一个接口,描述一组用于注册服务的方法。 这以较轻的方式模拟 .NET 依赖项注入服务集合功能,但实例而不是类型除外。
构造函数
| Service |
构造提供程序实例 |
方法
| add |
使用一组依赖项注册密钥的工厂。 |
| add |
注册密钥的工厂。 |
| add |
按密钥注册实例。 这将覆盖现有实例。 |
| compose |
为具有一组依赖项的密钥注册工厂(需要未定义的初始值)。 |
| compose |
注册一个工厂(需要未定义的初始值)作为密钥。 |
| make |
生成单个服务。 |
| make |
生成完整的服务集。 |
| must |
生成单个服务并断言它未定义。 |
| must |
生成完整的服务集,断言未定义指定的键。 |
构造函数详细信息
ServiceCollection(Record<string, unknown>)
构造提供程序实例
new ServiceCollection(defaultServices?: Record<string, unknown>)
参数
- defaultServices
-
Record<string, unknown>
默认的服务集
方法详细信息
addFactory<InstanceType, Dependencies>(string, string[], DependencyFactory<InstanceType, Dependencies, false>)
使用一组依赖项注册密钥的工厂。
function addFactory<InstanceType, Dependencies>(key: string, dependencies: string[], factory: DependencyFactory<InstanceType, Dependencies, false>): this
参数
- key
-
string
工厂将提供的密钥
- dependencies
-
string[]
此实例所依赖的事项集。 将通过 services提供给工厂功能。
- factory
-
DependencyFactory<InstanceType, Dependencies, false>
用于创建要提供的实例的函数
返回
this
用于链接的
addFactory<InstanceType>(string, Factory<InstanceType, false>)
注册密钥的工厂。
function addFactory<InstanceType>(key: string, factory: Factory<InstanceType, false>): this
参数
- key
-
string
工厂将提供的密钥
- factory
-
Factory<InstanceType, false>
用于创建要提供的实例的函数
返回
this
用于链接的
addInstance<InstanceType>(string, InstanceType)
按密钥注册实例。 这将覆盖现有实例。
function addInstance<InstanceType>(key: string, instance: InstanceType): this
参数
- key
-
string
正在提供的实例的键
- instance
-
InstanceType
要提供的实例
返回
this
用于链接的
composeFactory<InstanceType, Dependencies>(string, string[], DependencyFactory<InstanceType, Dependencies, true>)
为具有一组依赖项的密钥注册工厂(需要未定义的初始值)。
function composeFactory<InstanceType, Dependencies>(key: string, dependencies: string[], factory: DependencyFactory<InstanceType, Dependencies, true>): this
参数
- key
-
string
工厂将提供的密钥
- dependencies
-
string[]
此实例所依赖的事项集。 将通过 services提供给工厂功能。
- factory
-
DependencyFactory<InstanceType, Dependencies, true>
用于创建要提供的实例的函数
返回
this
用于链接的
composeFactory<InstanceType>(string, Factory<InstanceType, true>)
注册一个工厂(需要未定义的初始值)作为密钥。
function composeFactory<InstanceType>(key: string, factory: Factory<InstanceType, true>): this
参数
- key
-
string
正在提供的实例的键
- factory
-
Factory<InstanceType, true>
返回
this
用于链接的
makeInstance<InstanceType>(string, boolean)
生成单个服务。
function makeInstance<InstanceType>(key: string, deep?: boolean): InstanceType | undefined
参数
- key
-
string
要生成的服务
- deep
-
boolean
重新构造所有依赖项
返回
InstanceType | undefined
服务实例或未定义
makeInstances<InstancesType>()
生成完整的服务集。
function makeInstances<InstancesType>(): InstancesType
返回
InstancesType
所有已解析的服务
mustMakeInstance<InstanceType>(string, boolean)
生成单个服务并断言它未定义。
function mustMakeInstance<InstanceType>(key: string, deep?: boolean): InstanceType
参数
- key
-
string
要生成的服务
- deep
-
boolean
重新构造所有依赖项
返回
InstanceType
服务实例
mustMakeInstances<InstancesType>(string[])
生成完整的服务集,断言未定义指定的键。
function mustMakeInstances<InstancesType>(keys: string[]): InstancesType
参数
- keys
-
string[]
不得未定义的实例
返回
InstancesType
所有解析服务