Reputation: 2365
I have read many posts on the usage and differences between Service and Factory, including things such as "Factory is more sophisticated" or "You will use a Factory 80% of the time" or "Factory is more useful," or even "Factory...uses complex creation logic" and "Service...uses simple creation logic. My conundrum comes in the way that I have actually found usages for each type of service.
It appears to me that Service is actually capable of more complex operations than Factory. Here is some code to try to illustrate what I mean:
module.service('Record', function (){
this.retrieve = {
all: function(){
//retrieve all
},
record: function(id){
//retrieve individual record
}
};
this.save = function(){
try{
//save record or throw error
}
catch(e){
//catch block
}
finally{
//non-conditional action
}
};
this.clear = function(){
//clear records
};
this.test = function() {
//function used for testing record retrieval and storage
};
})
Note that I have left out all dependencies for simplicity. The one I use in my code is a Value service that contains values updated in the application, which are being retrieved and/or stored in localStorage.
I thought about what the equivalent of this code would be if I utilized Factory. I came up with this:
module.factory('Record', function (){
return {
retrieve: retrieve,
save: save,
clear: clear,
test: test
}
function retrieve(){
this.all = function(){
//retrieve all
}
this.record = function(id){
//retrieve individual record
}
}
function save(){
try{
//save record or throw error
}
catch(e){
//catch block
}
finally{
//non-conditional action
}
}
function clear(){
//clear records
}
function test() {
//function used for testing record retrieval and storage
}
});
The problem lies in the retrieve() function. Everything else works as it should, except that now, I cannot call retrieve from my controller like so:
Record.retrieve.all(); or Record.retrieve.record(1)
I cannot see any practical advantage to using Factory, despite all of the examples I've seen and everything I've read. So I fear that I do not understand it adequately enough.
For the example above, it sure seems that Service is a better choice. Does anybody have a real-life example where a Factory must be used for it to work properly, whereas a Service would not work?
A better way to rephrase this question, how would I correctly implement my code above into a Factory? (without creating separate functions for retrieveAll and retrieveRecord)?
Thanks for reading my post and for any insight you may have!
Upvotes: 1
Views: 64
Reputation: 19618
A factory can be used to create an injectable class and this can't be accomplished with a service, since angular use the "new
" operator to create an instance (singleton) of the service if the implementation returns a function rather than an object literal.
Example:
module.factory('Monster', function($log){
return function(name, skills) {
this.name = name;
this.skills = skills;
this.sayName = function() {
$log.info('Monster's name is:', this.name);
};
};
});
myapp.controller('MyController' function($scope, Monster){
var m1 = new Monster('Godzilla', ['fire']);
var m2 = new Monster('Gozer');
// do something with your instances...
});
the example above using a service will return an instance of Monster, not the constructor!
Upvotes: 4
Reputation: 48837
retrieve
should not be a function but an object instead:
module.factory('Record', function () {
return {
retrieve: {
all: function() {
// retrieve all
},
record: function(id) {
// retrieve individual record
}
},
save: function() {
try {
// save record or throw error
} catch(e) {
// catch block
} finally {
// non-conditional action
}
},
...
}
});
Upvotes: 2