import {LazyQuery, Queryable, QueryExpression} from '@dvolper/ts-collections' import {DependencyContainer} from '../dependency-container' import {DependencyCreator} from '../dependency-creator' export class DependencyQuery implements Queryable { private readonly _query: Queryable> public constructor ( private readonly _container: DependencyContainer, dependencies: DependencyCreator[], private readonly _args: any[] ) { this._query = new LazyQuery( dependencies ) } public* [ Symbol.iterator ] (): Generator { for( const dependency of this._query ) { yield this._container.serve( dependency, ...this._args ) } } public count (): number { return this._query.count() } public first (): TDependency { const first = this._query.first() return this._container.serve( first, ...this._args ) } public firstOrDefault ( defaultValue?: TDefault ): TDependency | TDefault { const firstOrDefault = this._query.firstOrDefault( defaultValue ) if( firstOrDefault == null || firstOrDefault === defaultValue ) { return defaultValue == null ? null : defaultValue } return this._container.serve( >firstOrDefault, ...this._args ) } public select ( expression: QueryExpression ): Queryable { const instances: TDependency[] = [] for( const dependency of this._query ) { instances.push( this._container.serve( dependency, ...this._args ) ) } return new LazyQuery( instances ).select( expression ) } public selectMany ( expression: QueryExpression ): Queryable { const instances: TDependency[] = [] for( const dependency of this._query ) { instances.push( this._container.serve( dependency, ...this._args ) ) } return new LazyQuery( instances ).selectMany( expression ) } public single (): TDependency { const single = this._query.single() return this._container.serve( single, ...this._args ) } public singleOrDefault ( defaultValue?: TDefault ): TDependency | TDefault { const singleOrDefault = this._query.singleOrDefault( defaultValue ) if( singleOrDefault == null || singleOrDefault === defaultValue ) { return defaultValue == null ? null : defaultValue } return this._container.serve( >singleOrDefault, ...this._args ) } public toArray (): TDependency[] { const instances: TDependency[] = [] for( const dependency of this._query ) { instances.push( this._container.serve( dependency, ...this._args ) ) } return instances } public where ( expression: QueryExpression ): Queryable { const instances: TDependency[] = [] for( const dependency of this._query ) { instances.push( this._container.serve( dependency, ...this._args ) ) } return new LazyQuery( instances ).where( expression ) } }