Reuse services without circular dependencies

I have one service globally storing all my data used in my application.

GlobalDataService (GDS)

angular .module('app.core') .service('GlobalDataService', GlobalDataService); GlobalDataService.$inject = ['$http', 'LineStatusService']; function GlobalDataService($http, LineStatusService) { var gds = this; gds.data = { //all my data } gds.data.lines = LineStatusService.getLineStatus().then... } 

And a simple crud service that processes the state of my data.

StatusDataService (SDS)

 angular .module('app.core') .service('LineStatusService', LineStatusService); LineStatusService.$inject = ['$http', 'GlobalDataService']; function LineStatusService($http, GlobalDataService) { var service = { getLineStatus: getLineStatus, saveLineStatus: saveLineStatus, ... }; function saveLineStatus (line, status, user) { var data = { status: { status_id: status.status_id, status_desc: status.status_desc }, updated_by: user } return $http.post('/api/euauto/v1/delivery-status/linestatus', data) .then(function successCallback(response) { GlobalDataService.data[id].status = status; return response.data; }).catch(function errorCallback(response) { }); } return service; } 

GDS should request all the state when the application loads first, then the state service processes any other data requests.

Now I understand that you do not have circular dependencies, so my plan was for my Controller to handle saving and updating using SDS and ALSO to update GDS.

Potential solution

 angular .module('core') .controller('MyController', MyController); MyController.$inject = ['GlobalDataService', 'LineStatusService']; function MyController(GlobalDataService, LineStatusService) { function changeStatus(line, status, user) { //do a thing //and another LineStatusService.saveLineStatus(line, status, user); GlobalDataService.data.line[id] = status; GlobalDataService.updateAllOtherData(); //etc... } } 

Problem

My question is: now I want to develop a new controller that contains the same functionality that I now have to remember in order to copy the same code and business logic from my original controller to reuse services . In addition, if GDS is not dependent on SDS, it will not be able to get LineStatus () at boot, and each controller in the application will have to remember getLineStatus () at boot.

Ideally, all logic and queries should be contained in one place , preferably my SDS. My GDS details must be consistent throughout the application.

+5
source share
1 answer

If you intend to initialize GlobalDataService with data when the application starts, you can initialize it in the .run() block, and not in the service constructor. Thus, GDS does not need other services to be introduced at all. In other data services, GDS can be introduced without cyclical dependency problems.

 angular .module('app.core') .run(function(GlobalDataService, LineStatusService) { GlobalDataService.data.lines = LineStatusService.getLineStatus().then... }); 
+4
source

Source: https://habr.com/ru/post/1271570/


All Articles