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

docs: add service doc #221

Merged
merged 1 commit into from
Jan 11, 2017
Merged

docs: add service doc #221

merged 1 commit into from
Jan 11, 2017

Conversation

leoner
Copy link
Contributor

@leoner leoner commented Jan 10, 2017

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
Description of change

@leoner leoner added this to the v1.x milestone Jan 10, 2017
```

### 注意事项
- Service 文件必须放在 `app/services` 目录,可以支持多级目录,访问的时候可以通过目录名级联访问。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

单数目录

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空行

@codecov-io
Copy link

codecov-io commented Jan 10, 2017

Current coverage is 97.78% (diff: 100%)

Merging #221 into master will decrease coverage by 0.10%

@@             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          

Powered by Codecov. Last update 96d5360...5cc1935

@popomore
Copy link
Member

service 的测试可以链到 unittest

---

# Service
`Service` 简单来说,就是当遇到一个复杂的业务场景,我们会把这些逻辑封装到一起,作为一个 `Service`,这样可以保持 Controller 的简单,而且也可以保证业务逻辑的独立性, 便于后面的重构和代码重用。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空行

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Service 小写,不用代码块

```

### 注意事项
- Service 文件必须放在 `app/services` 目录,可以支持多级目录,访问的时候可以通过目录名级联访问。
Copy link
Member

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` 的方式返回。
Copy link
Member

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里这个就可以不写了,定义的时候是让大家知道传 ctx 进来,一般可以不写


## Service 的一些场景

- 复杂数据的处理,比如要展现的信息需要从数据库获取,还要经过一定的规则计算,才能返回用户显示。或者计算完成后,更新到数据库。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这部分可能还要细化下, @dead-horse

@leoner leoner force-pushed the add-service-doc branch 3 times, most recently from 4fac2a8 to 15c75ca Compare January 10, 2017 09:15

# service

`service` 简单来说,就是当遇到一个复杂的业务场景,我们会把这些逻辑封装到一起,作为一个 `service`。这样可以可以有以下几个好处:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码块没去掉...


`service` 简单来说,就是当遇到一个复杂的业务场景,我们会把这些逻辑封装到一起,作为一个 `service`。这样可以可以有以下几个好处:

- 保持 Controller 中的逻辑更加简洁。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里都要小写


- service 文件必须放在 `app/service` 目录,可以支持多级目录,访问的时候可以通过目录名级联访问。

```
Copy link
Member

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
Copy link
Member

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` 来获取上下文相关信息。比如我们可以用:
Copy link
Member

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 的时候,传递进来。
Copy link
Member

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 重复调用。
Copy link
Member

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 {
constructor(ctx) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor 可以省略吧

Copy link
Member

@fengmk2 fengmk2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@leoner leoner force-pushed the add-service-doc branch 2 times, most recently from fa49d54 to 5f0cb9a Compare January 11, 2017 03:26

# service

service 简单来说,就是当遇到一个复杂的业务场景,我们会把这些逻辑封装到一起,作为一个 service。这样可以可以有以下几个好处:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样可以可以有以下几个好处: => 这样有以下几个好处:

Copy link
Member

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 的一些场景
Copy link
Member

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) {
Copy link
Member

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 的时候,传递进来。
Copy link
Member

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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注入了请求上下文

@popomore
Copy link
Member

这个搞定了么

@leoner
Copy link
Contributor Author

leoner commented Jan 11, 2017

上面说的, 应该都改好了, 再看下呀。


### Service ctx 详解

为了可以获取用户请求的链路,我们在 service 初始化中,注入了请求上下文, 用户在方法中可以通直接过 `this.ctx` 来获取上下文相关信息。关于上下文的具体详解可以参看 [Context](./extend.md#context),
Copy link
Member

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` 发起网络调用
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最后一个有句号,前面两行没有

@dead-horse
Copy link
Member

其他+1

@fengmk2 fengmk2 deleted the add-service-doc branch January 11, 2017 15:35
@popomore popomore mentioned this pull request Jan 12, 2017
31 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants