Angular-styleguide: Where to put services that don't have controllers neither views?

Created on 22 Mar 2016  路  3Comments  路  Source: johnpapa/angular-styleguide

For example: I have a StatesService. It returns all states of a country, but It doesn't have its own view.

  1. If It's only used in one view, should I put it into the subfolder of that view?
  2. If It's used in many views, where should I put it?

In "Folder-by-feature" https://github.com/johnpapa/angular-styleguide/tree/master/a1#folders-by-feature-structure, It can see a 'services' folder, but not in image below.

For moment, I'm using an "extra" folder to put services with no controllers nor views.

@johnpapa

Most helpful comment

Services don't relate to one View, they relate to a service or set of actions that they help provide for an app. So your scenario is quite common. You're already more than half way there :)

First we decide if we have the need for a service. You already did that with determining it handles state.

Then we decide how to extract ust that logic. You did that too!

Then we decide who needs it. Is it used by 1 controller? 1 service? 1 directive? Are they all in 1 feature/module? If used by one module I put it in that module. If used across the app, put it in a shared module.

Good luck

All 3 comments

  1. That's correct
  2. You should put it in the core service file core.service.js

I have a services directory that stores all of my common services.

Personally, I wouldn't put it in a "core service" file, because that seems like it would violate the Rule of 1 (Y001).

Services don't relate to one View, they relate to a service or set of actions that they help provide for an app. So your scenario is quite common. You're already more than half way there :)

First we decide if we have the need for a service. You already did that with determining it handles state.

Then we decide how to extract ust that logic. You did that too!

Then we decide who needs it. Is it used by 1 controller? 1 service? 1 directive? Are they all in 1 feature/module? If used by one module I put it in that module. If used across the app, put it in a shared module.

Good luck

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jejja picture jejja  路  25Comments

jansepke picture jansepke  路  12Comments

robrothedev picture robrothedev  路  6Comments

samithaf picture samithaf  路  12Comments

sgbeal picture sgbeal  路  7Comments