title: Service

Simply speaking, Service is an abstract layer which is used to encapsulate business logics in complex business circumstances, and this abstraction offers advantages as below:

  • keep logics in Controller cleaner.
  • keep business logics independent, since the abstracted Service can be called by many Controllers repeatedly.
  • separate logics and representations, and make it easier to write test cases. Write test cases in detail referring to here.

Usage Scenario

  • Processing complex data, e.g. information to be shown need to be got from databases, and should be processed in specific rules before it can be sent and seen by the user. Or when the process is done, the database should be updated.
  • Calling third party services, e.g. getting Github information etc.

Defining Service

  1. // app/service/user.js
  2. const Service = require('egg').Service;
  3. class UserService extends Service {
  4. async find(uid) {
  5. const user = await this.ctx.db.query('select * from user where uid = ?', uid);
  6. return user;
  7. }
  8. }
  9. module.exports = UserService;

Properties

Framework will initialize a new Service instance for every request accessing the server, and, for the example above, several attributes are attached to this since the Service class inherits egg.Service.

  • this.ctx: the instance of Context for current request, through which we can access many attributes and methods, encapsulated by the framework, of current request conveniently.
  • this.app: the instance of Application for current request, through which we can access global objects and methods provided by the framework.
  • this.service: Service defined by the application, through which we can access the abstract business layer, equivalent to this.ctx.service.
  • this.config: the application’s run-time config.
  • this.logger:logger with debuginfowarnerror, use to print different level logs, almost the same as context logger, but it will append Service file path for quickly track.

Service ctx in Detail

To get the path chain of user request, the request context is injected by us during the Service initialization, so you are able to get the related information of context directly by this.ctx in methods. For detailed information about context, please refer to Context.
With ctx, we can get various convenient attributes and methods encapsulated by the framework. For example we can use:

  • this.ctx.curl to make network calls.
  • this.ctx.service.otherService to call other Services.
  • this.ctx.db to make database calls etc, where db may be a module mounted by other plugins in advance.

Notes

  • Service files must be put under the app/service directory, and multi-level directory is supported, which can be accessed by cascading directory names.
  1. app/service/biz/user.js => ctx.service.biz.user
  2. app/service/sync_user.js => ctx.service.syncUser
  3. app/service/HackerNews.js => ctx.service.hackerNews
  • one Service file can only define one Class, which should be returned by module.exports.
  • Service should be defined in the Class way, and the parent class must be egg.Service.
  • Service is not a singleton but a request level object, the framework lazy-initializes it when the request ctx.service.xx for the first time, so the context of current request can be got from this.ctx in Service.

Using Service

We begin to see how to use Service from a complete example below.

  1. // app/router.js
  2. module.exports = app => {
  3. app.router.get('/user/:id', app.controller.user.info);
  4. };
  5. // app/controller/user.js
  6. const Controller = require('egg').Controller;
  7. class UserController extends Controller {
  8. async info() {
  9. const userId = ctx.params.id;
  10. const userInfo = await ctx.service.user.find(userId);
  11. ctx.body = userInfo;
  12. }
  13. }
  14. module.exports = UserController;
  15. // app/service/user.js
  16. const Service = require('egg').Service;
  17. class UserService extends Service {
  18. // the constructor is not a must by default
  19. // constructor(ctx) {
  20. // super(ctx); if some processes should be made in the constructor, this statement is a must in order to use `this.ctx` later
  21. // // get ctx through this.ctx directly
  22. // // get app through this.app directly too
  23. // }
  24. async find(uid) {
  25. // suppose we've got user's id and are going to get detailed user information from databases
  26. const user = await this.ctx.db.query('select * from user where uid = ?', uid);
  27. // suppose some complex processes should be made here, and demanded informations are returned then.
  28. const picture = await this.getPicture(uid);
  29. return {
  30. name: user.user_name,
  31. age: user.age,
  32. picture,
  33. };
  34. }
  35. async getPicture(uid) {
  36. const result = await this.ctx.curl(`http://photoserver/uid=${uid}`, { dataType: 'json' });
  37. return result.data;
  38. }
  39. }
  40. module.exports = UserService;
  41. // curl http://127.0.0.1:7001/user/1234