r/angularjs • • Mar 11 '16

.service() versus .factory(), the actual answer

https://toddmotto.com/factory-versus-service
64 Upvotes

21 comments sorted by

2

u/colonel_phorbins Mar 12 '16

Great stuff, Todd - thanks.

1

u/toddmotto Mar 12 '16

My pleasure!

9

u/gabbsmo Mar 11 '16

Always factory. I've given up on teaching my team how the this keyword works. Can't say I blame them though. Object factories is pretty damn intuitive once you get than functions are objects.

13

u/vivainio Mar 11 '16

Always service() here ;). Main reason is that service maps naturally to ES6 / TS class. So far we have had no reason instantiate that singleton any other way.

5

u/jax024 Mar 11 '16

Same here! I'd much rather it take a constructor than a basic object. I like the way the code looks more

1

u/joshmanders Mar 11 '16

3rdererederedederedered here

2

u/[deleted] Mar 11 '16

No. value() which returns a function which can be called instantiate a constructor function to access a singleton object is the way to do it.

1

u/vivainio Mar 12 '16

I saw what you did there

1

u/yesman_85 Mar 11 '16

Same here, we just do this:

app.factory('factoryName', [
    '$q', 'settings',
    ($q: ng.IQService, settings: ISettings): IItems =>
    new Items($q,settings)
]);

1

u/vivainio Mar 12 '16

You should be using service() for this pattern to avoid repeating your injectables. See https://github.com/Basware/typescript-ng1-style#services

1

u/yesman_85 Mar 12 '16

I have recently discovered this yes, and yes the repeating injectables is very annoying. But we have project with more than 500 factories so I'm hesitant to refactor at this point.

6

u/adropofhoney Mar 11 '16

Do they still have these in Angular 2.0?

1

u/kogsworth Mar 15 '16

no they don't

1

u/jacksonmills Mar 11 '16

I use services as "services, API services, data services."

I use factories as modules/functions that extend the functionality of the services.

For instance, in most of my current project's API services, I want some object lifecycle functionality. That stuff gets extended in my factories, and called by my services.

In other words, the factories build the services, and I use services the factories build in my code.

1

u/xmsxms Mar 12 '16

Nice explanation, I agree with the limited usefulness of existing explanations out there. Cheers

1

u/[deleted] Mar 12 '16

I still find the examples with the http calls to be unneeded for these kinds of explanations.

Its just simple: a service is not persistent, factories are.

If you have a value named x which starts with the value 5. And if you have a service to return x = x + 5, it will always return 10. If you have a factory, the first call will be 10, then 15, then 20, etc. Basically the value x will be persistent. Factories are really handy to store data between pages, directives and such. You could store the user-data in a factory or keep data from a multi-page form. Where in the service you would probably set the $http calls to submit the data as that doesn't need to be persistent (but is handy to have all calls in a single file for maintenance).

1

u/kogsworth Mar 15 '16

I just tried every single example that you give in your article and putting .service instead of .factory and they all behave in the exact same way.
When you declare something with new, the constructor function will either return whatever you add as an explicit return or implicitly return this. I really don't see any need to use .factory at all, you can literally just replace the word with .service and everything will still work.
Why not always use service since they map really easily to ES6/TS classes?

-1

u/Chun Mar 12 '16

The "actual answer" is to only use factories. Even then, only when you have to. I think angular's "DI for everything" is a failed experiment, ES6 imports lead to much cleaner code.

0

u/[deleted] Mar 11 '16

[deleted]

7

u/toddmotto Mar 11 '16

Services are still a single instance, they're only invoked once, same with factory. Basically .service() is new'd and then when you inject it, you're getting that same instance, not a new one.

0

u/[deleted] Mar 11 '16

[deleted]

1

u/toddmotto Mar 12 '16

Hehe, hey I'm no horse ;) but yesssss! Hope that helped and happy coding!

0

u/[deleted] Mar 11 '16

[deleted]

1

u/vivainio Mar 12 '16

If you are using ES6 classes (or ES5 emulation of the same) as services, use service().

Use factory() if your team is politically opposed to ctor functions and classes (e.g. they want to create services by object literals or factory functions)