Skip to content

Instantly share code, notes, and snippets.

@DScheglov
Last active October 23, 2020 18:52
Show Gist options
  • Save DScheglov/c5f357805cbc7ff76d5af6e6bc6b19df to your computer and use it in GitHub Desktop.
Save DScheglov/c5f357805cbc7ff76d5af6e6bc6b19df to your computer and use it in GitHub Desktop.
interface IServices {
logger: ILogger,
dataAccessService: IDataAccessService,
ecommerceService: IECommerceService,
}
class ServiceLocator implements IServices {
private _logger: ILogger | null = null;
private _dataAccessService: IDataAccessService | null = null;
private _ecommerceService: IECommerceService | null = null;
get logger(): ILogger {
if (this._logger === null) {
this._logger = new Logger();
}
return this._logger;
}
set logger(value) {
if (value != null) throw new Error("You cannot replace an item in the container");
this._logger = null;
}
get dataAccessService() {
if (this._dataAccessService === null) {
this._dataAccessService = new DataAccessService(this.logger);
}
return this._dataAccessService
}
set dataAccessService(value) {
if (value != null) throw new Error("You cannot replace an item in the container");
this._dataAccessService = null;
}
get ecommerceService() {
if (this._ecommerceService === null) {
this._ecommerceService = new ECommerceService(this.logger, this.dataAccessService);
}
return this._ecommerceService;
}
set ecommerceService(value) {
if (value != null) throw new Error("You cannot replace an item in the container");
this._ecommerceService = null;
}
}
export default new ServiceLocator();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment