Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Design Proposal: Modularize AngularJS Service Creation #214

Closed
jniles opened this issue Mar 24, 2016 · 2 comments · Fixed by #421
Closed

Design Proposal: Modularize AngularJS Service Creation #214

jniles opened this issue Mar 24, 2016 · 2 comments · Fixed by #421

Comments

@jniles
Copy link
Collaborator

jniles commented Mar 24, 2016

The majority of our client-side services implement CRUD on the backend database using HTTP verbs against a single API route. Almost every single service implements their read() and delete() methods identically. In general, they look like this:

var baseUrl = 'some/route/';

// read values from the database
function read(id, params) {
   var url = baseUrl.concat(id || '');
   return $http.get(url, { params : params })
   .then(util.unwrapHttpResponse);
}

// delete a record on the server
function del(id) {
  return $http.delete(baseUrl + id)
  .then(util.unwrapHttpResponse);
}

Since we have many services, this results in a lot of repeated code. This means more code to send to the client (larger JS payload) and maintain as a development team.

Here's an idea I'd like feedback on for maintaining this code more easily. We should simply inherit the identical methods from a prototype service using the awesome power of angular.extend(). For example, here is how you might create the prototype service and use it to create several services:

angular.module('bhima.services')
.service('PrototypeService', PrototypeService);

// this is the prototype service that will provide a base for all other services
// TODO - this might be cleaner as a factory.  Depends how $http and util are inherited.
function PrototypeService($http,util) {
  var service = this;

  // make sure that the methods have access to $http and util
  service.$http = $http;
  service.util = util;

  // bind props
  service.read = read;
  service.delete = del;

  // read values from the database
  function read(id, params) {
     var url = this.url.concat(id || '');
    return this.$http.get(url, { params : params })
      .then(this.util.unwrapHttpResponse);
  }.bind(service);

  // delete a record on the server
  function del(id) {
    return this.$http.delete(service.url.concat(id))
      .then(this.util.unwrapHttpResponse);
  }.bind(service);

  return service;
}

// in another file
angular.module('bhima.services')
.service('SomeService', SomeService);

SomeService.$inject = [
  'PrototypeService', '$http', 'util'
];

// Some other service definition
function SomeService($http, util, PrototypeService) {
  var service = this;

   // the server API target
  service.url = 'some/route/endpoint/';

  // add the read() and delete() methods to this service.
  angular.extend(service, PrototypeService);

  // specific methods defined below
  service.update = function update() { /* etc ... */ };
  service.create = function create() { /* etc ... */ };

  return service;
}

If we have close to one service per database API, this will get very large, very quickly. Modularizing our code like this might be a good idea.

@IMA-WorldHealth/local-contributors what do you think?

@DedrickEnc
Copy link
Contributor

This is called inheritance (heritage) in OOP paradigm, it is good to make our code clean and easy to read and very rich in terms of design.

But it will also help us to save time in development because CRUD operation code will be factorized since it is common to many services.

I service will contain only method which are very specific to him.
Good thought, congratulation!

I am okay with that for conceptual perceptive and best practice.

Thanks

@jniles
Copy link
Collaborator Author

jniles commented Mar 26, 2016

@DedrickEnc , thanks for the feedback! We'll look at developing a PR that refactors a lot of the services when they are more stable.

jniles referenced this issue in jniles/bhima May 23, 2016
This commit implements PrototypeApiService to remove boilerplate code
from our services and improve code documentation. To demonstrate how the
PrototypeApiService works, the FiscalService has been created to inherit
directly from it.  The CashPayments service also is updated to inherit
from the PrototypeApiService.

This also increments mocha's versioning since the recent bug fix.

Finally, a bug with the bhCurrencySelect has been fixed.

Closes #214.
jniles referenced this issue in jniles/bhima May 23, 2016
This commit implements PrototypeApiService to remove boilerplate code
from our services and improve code documentation. To demonstrate how the
PrototypeApiService works, the FiscalService has been created to inherit
directly from it.  The CashPayments service also is updated to inherit
from the PrototypeApiService.

This also increments mocha's versioning since the recent bug fix.

Finally, a bug with the bhCurrencySelect has been fixed.

Closes #214.
jniles referenced this issue in jniles/bhima May 25, 2016
This commit implements PrototypeApiService to remove boilerplate code
from our services and improve code documentation. To demonstrate how the
PrototypeApiService works, the FiscalService has been created to inherit
directly from it.  The CashPayments service also is updated to inherit
from the PrototypeApiService.

This also increments mocha's versioning since the recent bug fix.

Finally, a bug with the bhCurrencySelect has been fixed.

Closes #214.
jniles referenced this issue in jniles/bhima May 25, 2016
This commit implements PrototypeApiService to remove boilerplate code
from our services and improve code documentation. To demonstrate how the
PrototypeApiService works, the FiscalService has been created to inherit
directly from it.  The CashPayments service also is updated to inherit
from the PrototypeApiService.

This also increments mocha's versioning since the recent bug fix.

Finally, a bug with the bhCurrencySelect has been fixed.

Closes #214.
jniles referenced this issue in jniles/bhima May 25, 2016
This commit implements PrototypeApiService to remove boilerplate code
from our services and improve code documentation. To demonstrate how the
PrototypeApiService works, the FiscalService has been created to inherit
directly from it.  The CashPayments service also is updated to inherit
from the PrototypeApiService.

This also increments mocha's versioning since the recent bug fix.

Finally, a bug with the bhCurrencySelect has been fixed.

Closes #214.
jniles referenced this issue in jniles/bhima May 25, 2016
This commit implements PrototypeApiService to remove boilerplate code
from our services and improve code documentation. To demonstrate how the
PrototypeApiService works, the FiscalService has been created to inherit
directly from it.  The CashPayments service also is updated to inherit
from the PrototypeApiService.

This also increments mocha's versioning since the recent bug fix.

Finally, a bug with the bhCurrencySelect has been fixed.

Closes #214.
jniles referenced this issue in jniles/bhima May 27, 2016
This commit implements PrototypeApiService to remove boilerplate code
from our services and improve code documentation. To demonstrate how the
PrototypeApiService works, the FiscalService has been created to inherit
directly from it.  The CashPayments service also is updated to inherit
from the PrototypeApiService.

This also increments mocha's versioning since the recent bug fix.

Finally, a bug with the bhCurrencySelect has been fixed.

Closes #214.
jniles referenced this issue in jniles/bhima May 27, 2016
This commit implements PrototypeApiService to remove boilerplate code
from our services and improve code documentation. To demonstrate how the
PrototypeApiService works, the FiscalService has been created to inherit
directly from it.  The CashPayments service also is updated to inherit
from the PrototypeApiService.

This also increments mocha's versioning since the recent bug fix.

Finally, a bug with the bhCurrencySelect has been fixed.

Closes #214.
@jniles jniles mentioned this issue Aug 27, 2016
11 tasks
jniles referenced this issue in jniles/bhima Jan 30, 2017
This commit version bumps the versions of protractor, dotenv, and
ui-grid.  It also ensures Travis CI only tests on relevant NodeJS
versions.

Closes #214.
jniles referenced this issue in jniles/bhima Jan 30, 2017
This commit version bumps the versions of protractor, dotenv, and
ui-grid.  It also ensures Travis CI only tests on relevant NodeJS
versions.

Closes #214.
jniles referenced this issue in jniles/bhima Jan 30, 2017
This commit version bumps the versions of protractor, dotenv, and
ui-grid.  It also ensures Travis CI only tests on relevant NodeJS
versions.

Closes #214.
jniles referenced this issue in jniles/bhima Jan 30, 2017
This commit version bumps the versions of protractor, dotenv, and
ui-grid.  It also ensures Travis CI only tests on relevant NodeJS
versions.

Closes #214.
jniles referenced this issue in jniles/bhima Jan 30, 2017
This commit version bumps the versions of protractor, dotenv, and
ui-grid.  It also ensures Travis CI only tests on relevant NodeJS
versions.

Closes #214.
jniles referenced this issue in jniles/bhima Jan 30, 2017
This commit version bumps the versions of protractor, dotenv, and
ui-grid.  It also ensures Travis CI only tests on relevant NodeJS
versions.

Closes #214.
bors bot added a commit that referenced this issue Sep 9, 2018
3167: Update snyk to the latest version 🚀 r=jniles a=greenkeeper[bot]




## Version **1.95.3** of **snyk** was just published.

<table>
  <tr>
    <th align=left>
      Dependency
    </th>
    <td>
      <a target=_blank href=https://github.com/snyk/snyk>snyk</a>
    </td>
  </tr>
  <tr>
      <th align=left>
       Current Version
      </th>
      <td>
        1.95.1
      </td>
    </tr>
  <tr>
    <th align=left>
      Type
    </th>
    <td>
      dependency
    </td>
  </tr>
</table>



The version **1.95.3** is **not covered** by your **current version range**.

If you don’t accept this pull request, your project will work just like it did before. However, you might be missing out on a bunch of new features, fixes and/or performance improvements from the dependency update.

It might be worth looking into these changes and trying to get this project onto the latest version of snyk.

If you have a solid test suite and good coverage, a passing build is a strong indicator that you can take advantage of these changes directly by merging the proposed change into your project. If the build fails or you don’t have such unconditional trust in your tests, this branch is a great starting point for you to work on the update.


---


<details>
<summary>Release Notes</summary>
<strong>v1.95.3</strong>

<h2><a href="https://urls.greenkeeper.io/snyk/snyk/compare/v1.95.2...v1.95.3">1.95.3</a> (2018-09-09)</h2>
<h3>Bug Fixes</h3>
<ul>
<li>allow silent clearing of non-existent spinner labels (<a href="https://urls.greenkeeper.io/snyk/snyk/commit/fa520a3">fa520a3</a>)</li>
<li>better readability on monitor output (<a href="https://urls.greenkeeper.io/snyk/snyk/commit/f455f65">f455f65</a>)</li>
<li>exit code 1 on snyk monitor failure (<a href="https://urls.greenkeeper.io/snyk/snyk/commit/1a30329">1a30329</a>)</li>
</ul>
</details>

<details>
<summary>Commits</summary>
<p>The new version differs by 6 commits.</p>
<ul>
<li><a href="https://urls.greenkeeper.io/snyk/snyk/commit/4a20b1d23eb0484c4865df71215ed676d351bed4"><code>4a20b1d</code></a> <code>Merge pull request #215 from snyk/feat/exit-code-1-on-monitor-failure</code></li>
<li><a href="https://urls.greenkeeper.io/snyk/snyk/commit/f455f65c3931c01d63b374eeaa237d827857236b"><code>f455f65</code></a> <code>fix: better readability on monitor output</code></li>
<li><a href="https://urls.greenkeeper.io/snyk/snyk/commit/1a303298259a41e4506bfd758516785efd46fb2b"><code>1a30329</code></a> <code>fix: exit code 1 on snyk monitor failure</code></li>
<li><a href="https://urls.greenkeeper.io/snyk/snyk/commit/fa520a3b2936046a38b199421449b7b2f3b12dfb"><code>fa520a3</code></a> <code>fix: allow silent clearing of non-existent spinner labels</code></li>
<li><a href="https://urls.greenkeeper.io/snyk/snyk/commit/fb1d018734cec7c06b143ca8e477e6b23f488306"><code>fb1d018</code></a> <code>Merge pull request #214 from snyk/fix/clear-spinner-on-errors</code></li>
<li><a href="https://urls.greenkeeper.io/snyk/snyk/commit/6f0f37fb391b273ee9851fb482bf04bcc9cd88cf"><code>6f0f37f</code></a> <code>fix: clear spinner labels on errors</code></li>
</ul>
<p>See the <a href="https://urls.greenkeeper.io/snyk/snyk/compare/f65072913187d8e34d6fb9ceaae1f7dc576cef39...4a20b1d23eb0484c4865df71215ed676d351bed4">full diff</a></p>
</details>

<details>
  <summary>FAQ and help</summary>

  There is a collection of [frequently asked questions](https://greenkeeper.io/faq.html). If those don’t help, you can always [ask the humans behind Greenkeeper](https://github.com/greenkeeperio/greenkeeper/issues/new).
</details>

---


Your [Greenkeeper](https://greenkeeper.io) bot 🌴



Co-authored-by: greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants