0

Lets say I have UserService. I have another module that depends on abstract PersonService. How can I substitute my own UserService to the PersonService?

So, I want some link UserService (container name of service) to the PersonService (container name of service). How do I do that?

what do I call abstract service?

Lets say I have PhotoModule. PhotoModule is only about PHOTOS. But PhotoModule also needs some user functionality.

Lets say I have UserModule. UserModule is only about users; it does not know anything about photos and PhotoModule.

How do we resolve this dependencies without mixing them?

Lets say PhotoModule has abstract service PersonService. Yes, there is no abstract service in angular, but I name it so because it does not have function implementation - only service name. PhotoModule needs, for example PersonService.getCurrentUser().

Such as PersonService.getCurrentUser does not have implementation body, but UserService.getCurrentUser have (from UserModule), I want to substitute PersonService with UserService.

But still, PhotoModule does now know about UserService, but knows about PersonService. My point is to substitute PersonService with UserService on the application configuration level to resolve this dependencies.

pleerock
  • 18,322
  • 16
  • 103
  • 128
  • 1
    do you mean `angular.module('my_services').factory('UserService', function (PersonService) {...}` ? ` – akonsu Feb 21 '14 at 17:14
  • no, I dont want to use one service in the another. I want something like this: `angular.module('my_services').factory('UserService', '@PersonService');`, e.g. service linking; or maybe `angular.module('my_services').link('UserService', 'PersonService');` – pleerock Feb 21 '14 at 17:21
  • please explain "service linking". what is "abstract service"? Angular does not have a notion of abstract service... – akonsu Feb 21 '14 at 17:35
  • maybe you can use `$provider.decorator`? I am not sure. http://stackoverflow.com/questions/16075982/angularjs-what-are-decorators-and-how-are-they-used – akonsu Feb 21 '14 at 18:27
  • Im looking for better solution if it exists, if no then probably I'll try decorators. Please post your comment as an answer – pleerock Feb 21 '14 at 20:24

1 Answers1

0

So, I want some link UserService (container name of service) to the PersonService (container name of service). How do I do that?

The issue is with your domain model, ask yourself what is the real difference in Person/User? Do you Really need to have two different services?

This is how I would approach the situation IF I truly needed to separate them

angular.module('app').service('personService', function(){

    // Logic for person service
    var getPersonDetails = function(){ /*get person logic*/ };

    return {
      GetPersonDetails: getPersonDetails
    };
});

// If you want to have 'base' functionality then just inject the person 'base' 
// into your user service.
angular.module('app').service('userService', ['personService', function(personService){

    // Logic for user service
    // Expose/override Logic for personService
    var getUser = function() { /*get user logic*/ };
    var getPersonDetails = function() { return personService.GetPersonDetails(); };

    return {
      GetUser: getUser,
      GetPersonDetails: getPersonDetails
    };

}]);

angular.module('app').controller('photoController', ['$scope', 'userService',
    function($scope, userService){

    // now if you only need to know about the person service 
    // then just inject the person service
    // if you need to know about the user AND person
    // inject the user service and do whatever

}]);

If you want to create a specific service on demand then use the Factory Pattern, (not an angular factory but a service that implements the Factory Pattern)

Example:

var userService = userFactory.getService('user');
// or
var personService = userFactory.getService('person');