-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
docs: add service doc #221
Conversation
3e6a769
to
3c480fa
Compare
``` | ||
|
||
### 注意事项 | ||
- Service 文件必须放在 `app/services` 目录,可以支持多级目录,访问的时候可以通过目录名级联访问。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
单数目录
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
空行
Current coverage is 97.78% (diff: 100%)@@ master #221 diff @@
==========================================
Files 34 34
Lines 900 948 +48
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 881 927 +46
- Misses 19 21 +2
Partials 0 0
|
service 的测试可以链到 unittest |
--- | ||
|
||
# Service | ||
`Service` 简单来说,就是当遇到一个复杂的业务场景,我们会把这些逻辑封装到一起,作为一个 `Service`,这样可以保持 Controller 的简单,而且也可以保证业务逻辑的独立性, 便于后面的重构和代码重用。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
空行
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Service 小写,不用代码块
``` | ||
|
||
### 注意事项 | ||
- Service 文件必须放在 `app/services` 目录,可以支持多级目录,访问的时候可以通过目录名级联访问。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
空行
app/services/biz/user.js => this.services.biz.user.find | ||
``` | ||
|
||
- 一个 Service 文件只能包含一个类, 这个类需要通过 `module.exports` 的方式返回。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
小写?
// app/services/user.js | ||
module.exports = app => { | ||
class User extends app.Service { | ||
constructor(ctx) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里这个就可以不写了,定义的时候是让大家知道传 ctx 进来,一般可以不写
|
||
## Service 的一些场景 | ||
|
||
- 复杂数据的处理,比如要展现的信息需要从数据库获取,还要经过一定的规则计算,才能返回用户显示。或者计算完成后,更新到数据库。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这部分可能还要细化下, @dead-horse
4fac2a8
to
15c75ca
Compare
|
||
# service | ||
|
||
`service` 简单来说,就是当遇到一个复杂的业务场景,我们会把这些逻辑封装到一起,作为一个 `service`。这样可以可以有以下几个好处: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
代码块没去掉...
|
||
`service` 简单来说,就是当遇到一个复杂的业务场景,我们会把这些逻辑封装到一起,作为一个 `service`。这样可以可以有以下几个好处: | ||
|
||
- 保持 Controller 中的逻辑更加简洁。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里都要小写
|
||
- service 文件必须放在 `app/service` 目录,可以支持多级目录,访问的时候可以通过目录名级联访问。 | ||
|
||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
js
为了可以获取用户请求的链路,我们在 Service 中,注入了 ctx, 用户直接可以通过 `this.ctx` 来获取上下文相关信息。比如我们可以用: | ||
|
||
- `this.ctx.curl` 发起网络调用 | ||
- `this.ctx.service.otherService` 调用其他 Service |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
小写
|
||
### Service ctx 详解 | ||
|
||
为了可以获取用户请求的链路,我们在 Service 中,注入了 ctx, 用户直接可以通过 `this.ctx` 来获取上下文相关信息。比如我们可以用: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Service 小写
``` | ||
|
||
- 一个 service 文件只能包含一个类, 这个类需要通过 `module.exports` 的方式返回。 | ||
- service 需要通过 Class 的方式定义,父类必须是 app.Service, 其中 app.Service 会在初始化 Service 的时候,传递进来。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
app.Service 加代码块
Service 小写
`service` 简单来说,就是当遇到一个复杂的业务场景,我们会把这些逻辑封装到一起,作为一个 `service`。这样可以可以有以下几个好处: | ||
|
||
- 保持 Controller 中的逻辑更加简洁。 | ||
- 保持业务逻辑的独立性,抽象出来的 Service 可以被多个 Controller 重复调用。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
小写
15c75ca
to
578fa09
Compare
```js | ||
module.exports = app => { | ||
class User extends app.Service { | ||
constructor(ctx) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constructor 可以省略吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
fa49d54
to
5f0cb9a
Compare
|
||
# service | ||
|
||
service 简单来说,就是当遇到一个复杂的业务场景,我们会把这些逻辑封装到一起,作为一个 service。这样可以可以有以下几个好处: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这样可以可以有以下几个好处:
=> 这样有以下几个好处:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
简单来说,service 就是在复杂业务场景下用于做业务逻辑封装的一个抽象层。
- 保持业务逻辑的独立性,抽象出来的 service 可以被多个 controller 重复调用。 | ||
- 将逻辑和展现分离,更容易编写测试用例,测试用例的编写具体可以查看 [这里](../core/unittest.md)。 | ||
|
||
## service 的一些场景 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## 使用场景
```js | ||
module.exports = app => { | ||
class User extends app.Service { | ||
* find(uid) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里面给一个简单的伪代码例子吧,例如从 db 中查出来
``` | ||
|
||
- 一个 service 文件只能包含一个类, 这个类需要通过 `module.exports` 的方式返回。 | ||
- service 需要通过 Class 的方式定义,父类必须是 `app.Service`, 其中 `app.Service` 会在初始化 service 的时候,传递进来。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
会在初始化 service 的时候,传递进来。
=> 会在初始化 service 的时候通过参数传递进来。
|
||
### Service ctx 详解 | ||
|
||
为了可以获取用户请求的链路,我们在 service 初始化中,注入了 ctx, 用户在方法中可以通直接过 `this.ctx` 来获取上下文相关信息。关于上下文的具体详解可以参看 [Context](./extend.md#context), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注入了请求上下文
5f0cb9a
to
5cc1935
Compare
这个搞定了么 |
上面说的, 应该都改好了, 再看下呀。 |
|
||
### Service ctx 详解 | ||
|
||
为了可以获取用户请求的链路,我们在 service 初始化中,注入了请求上下文, 用户在方法中可以通直接过 `this.ctx` 来获取上下文相关信息。关于上下文的具体详解可以参看 [Context](./extend.md#context), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
通直接过 -> 直接通过
为了可以获取用户请求的链路,我们在 service 初始化中,注入了请求上下文, 用户在方法中可以通直接过 `this.ctx` 来获取上下文相关信息。关于上下文的具体详解可以参看 [Context](./extend.md#context), | ||
有了 ctx 我们可以拿到框架给我们封装的各种便捷属性和方法。比如我们可以用: | ||
|
||
- `this.ctx.curl` 发起网络调用 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最后一个有句号,前面两行没有
其他+1 |
5cc1935
to
77dae2f
Compare
Checklist
npm test
passesAffected core subsystem(s)
Description of change