I use services as "data handlers". Stuff that talks to the server, stuff
that holds data to communicate among directives, etc. I also use services
for things like directives that can be summoned by any other directive - a
fancy alert popup, for example, would be accessed through a service.

I use factories for extending services. I use Restangular so I often give
certain element transformers to all objects returning back from the server.
Factories return functions and it feels more natural to pass those objects
to them.

I rarely use providers, but I typically use them when I have a service
which is used over several apps in our code base and it needs some
configuration in order to work with each one.

90% of the time it's a service. 7% of the time it's a factory, 3% of the
time, a provider.

On Fri, Apr 3, 2015 at 12:15 PM, a_gaur <[email protected]>
wrote:

> I have been really confused when to use factories or services, this SO
> question
> <http://stackoverflow.com/questions/18939709/when-to-use-service-instead-of-factory>
>  gives
> a great insight over the difference but I still don't understand over their
> usage. One of the answers gives a very good conclusion of deciding what to
> use when :
>
> In conclusion,
>
> ---------------------------------------------------  | Type    | Singleton| 
> Instantiable | 
> Configurable|---------------------------------------------------  | Service | 
> Yes      | No           | No          
> |---------------------------------------------------  | Factory | Yes      | 
> Yes          | No          
> |---------------------------------------------------  | Provider| Yes      | 
> Yes          | Yes         |       
> ---------------------------------------------------
>
>
>    1.
>
>    Use Service when you need just a simple object such as a Hash, for
>    example {foo;1, bar:2} It’s easy to code, but you cannot instantiate it.
>    2.
>
>    Use Factory when you need to instantiate an object, i.e new
>    Customer(), new Comment(), etc.
>    3.
>
>    Use Provider when you need to configure it. i.e. test url, QA url,
>    production url.
>
>
> However what was the initial purpose of introducing services, I saw the
> angular code and it seems both of them are same. In fact one is created
> from another doing some debugging I found out that services are created
> from factories and factories are created from providers. We just default
> some values as go down the chain for instance in service you only can
> create an object while in a factory you can create a hash, a value or an
> object but the configuration is default and lastly in provider you can also
> pass the configuration block.
> Then what led to creation of services and factories when providers can do
> everything and even more w.r.t. the two ?
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to