0

I'm writing an app, which does OAuth authentication to several providers (e.g. twitter, facebook). I'm new to Angular, the UI / model is written in Angular without an issue. But, I've got a lot of helper methods, e.g. callApi, checkVerification, getToken, etc. etc. I think about 8 methods. I've been looking into creating factories in AngularJS, but I don't know why I should do this? It will require a lot of rewriting and I don't see the advantage? Why not keep it simple with helper methods?

Boland
  • 1,531
  • 1
  • 14
  • 42
  • Factories are nice because you can inject them wherever you need them...you write your client data access layer once in a factory and then re-use it where needed. – tymeJV Sep 25 '14 at 19:15
  • @Boland, I agree with you I wondered the same and my conclusion is that it is matter of preference. There is no right answer, although it is open to discussion and I am not saying that factories are bad or wrong either. If want to check what is some accepted [Angular Best Practices](http://stackoverflow.com/questions/20802798/angularjs-code-naming-conventions) – Dalorzo Sep 25 '14 at 19:16
  • 2
    I think you meant why should I use Services in Angular for which there is an SO answer already http://stackoverflow.com/questions/16709421/why-use-services-in-angular – mccainz Sep 25 '14 at 19:17
  • I like mccainz answer, stupid I did not find that answer. Only searched for factories... – Boland Sep 25 '14 at 19:22
  • Helper methods, asin, functions defined in your controller? The purpose of moving those to a factory or service would simply be so that you can re-use them without repeating code. In more recent applications i find myself moving more and more of my logic out of controllers and into services. Controllers simply expose methods and properties from the services to the view. The only logic that ends up in my controller tends to be things that aren't relevant to the services, such as pagination/filtering/sorting – Kevin B Sep 25 '14 at 19:24
  • Yeah, I did not put them into the controller, but just as separate functions in a js file. Thanks everyone. – Boland Sep 25 '14 at 19:27
  • possible duplicate of [Service vs provider vs factory?](http://stackoverflow.com/questions/15666048/service-vs-provider-vs-factory) – Endless Sep 25 '14 at 19:31

1 Answers1

1

A factory is just one way of creating an angular service. You can group your helper methods into these services.

Being a angular programmer and not using services is exactly like being a electrician and not using the ground wire, its not really needed for anything and saves time to not have to hook it up, but you are taking risks of starting a fire if some appliance has a short. In programming the risks are in terms of code flexibility, maintainability, and test-ability. If one of your modules does not work as intended, it will be harder to debug, etc.

When you have a significant amount of code, logical groupings of helper functions into services helps you more easily organize or find them and test them.

This best practice is suggested by many senior developers who worked on large angularjs projects and have had successful experiences by structuring code in this way. It provides another layer into which you may find you want to insert code later. If you don't have it in place, you won't use it (for the reason you just said, it will take you time to rewrite) and then your code will just end up messier and messier over time. A major feature of angular is to encourage code organization and test-ability.

Of course, you can write code any way you want that works, but using services (via factory or other methods) will help you not end up with large project that becomes a very difficult to maintain mess over the long term. Like other design patterns, very smart programmers have discovered coding structures and best practices that yield powerful long term maintainability benefits.

See: Why use services in Angular?

for more views on this.

Community
  • 1
  • 1
pilavdzice
  • 958
  • 8
  • 27