* Resolve a service * 解析服务
(identifier: ServiceIdentifier<T>)
| 116 | * 解析服务 |
| 117 | */ |
| 118 | public resolve<T>(identifier: ServiceIdentifier<T>): T { |
| 119 | this.checkDisposed(); |
| 120 | |
| 121 | const registration = this._registrations.get(identifier); |
| 122 | if (!registration) { |
| 123 | throw new Error(`Service not registered: ${identifier.name}`); |
| 124 | } |
| 125 | |
| 126 | // Check for circular dependency |
| 127 | if (this._resolving.has(identifier)) { |
| 128 | throw new Error(`Circular dependency detected: ${identifier.name}`); |
| 129 | } |
| 130 | |
| 131 | // Return cached singleton if available |
| 132 | if (registration.lifecycle === EServiceLifecycle.Singleton && registration.instance !== undefined) { |
| 133 | return registration.instance as T; |
| 134 | } |
| 135 | |
| 136 | // Resolve |
| 137 | this._resolving.add(identifier); |
| 138 | try { |
| 139 | const instance = registration.factory(this) as T; |
| 140 | |
| 141 | if (registration.lifecycle === EServiceLifecycle.Singleton) { |
| 142 | registration.instance = instance; |
| 143 | } |
| 144 | |
| 145 | return instance; |
| 146 | } finally { |
| 147 | this._resolving.delete(identifier); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Try to resolve a service, returns null if not found |
no test coverage detected