Giter Site home page Giter Site logo

Comments (6)

taoso avatar taoso commented on May 8, 2024 4

我们自底向上看。

dao 层是数据访问层,包括数据库访问(分库分表等)、接口调用、缓存读写等。这一层的基本职能就是向上屏蔽数据访问细节。上导可以统一读写数据,而无须考虑数据的存储形式。一般来说一张表(及其附属表或扩展表)一个dao,缓存操作也放到同一个dao里。

所有的数据统一走dao层,service 导就会比较清真。一般一个 service 实现一段完整的业务逻辑。一段业务逻辑基本都需要操作多个dao。以漫画阅读为例,我们需通过 dao/comic 查询漫画信息,我们需要 dao/user 查询用户信息及其解锁信息,我们需要 dao/location 查询当前用户的归属地进而判断该漫画是否可以在该地区阅读。整体的流程都需要在 service 这一层完成。

server 目前的主要职责是参数转化和基本校验。比如,我们用户平台的入参可能是1、2、3,但在系统内使用 PlatformWeb、PlatformAndroid、PlatformIOS 表示,这种转化就需要在 server 完成。server 需要将来自传输层(tcp/http)的序列化数据转化成 service 层需要的逻辑数据,反之亦然。一般来说,一个 server 调一个 service, 也就是说 server 层尽量不要写太多的业务逻辑。

rpc 参数传递的问题确实有点争议。从 server 的职能来看,我们不应该将 rpc 年成的 pb 对象传给 service层,而是要在 server 层统一做转化。这样 service 就可以跟 rpc 层彻底解耦。但在实践中,我们发现在 server 做转化太烦琐,所以我们在 server 层和 service 层复用 rpc 的 pb 对象。

但有 dao 层需要特别注意。dao 层是面向底层数据的。我们在 rpc 导定接口的时候面向的是功能,可能包含漫画、用户、消费等好几类字段。但对于一个 dao,它只能包含一类信息,要么是漫画,要么是用户。所以,rpc的对象是没法用在 dao 导层。

from sniper.

flazx avatar flazx commented on May 8, 2024

太感谢了~~ @lvht

from sniper.

flazx avatar flazx commented on May 8, 2024

@lvht dao层传递到server层的数据转换应当在哪一层做比较合适呢?从您的回复来看,应当是放到server层?

例如:getUserDetail这个数据传递:
dao(dao.UserDetail) --> service(dao.UserDetail) --> server( dao.UserDetail ---> pg.UserDetail)
这个dao层数据传递到server层才做转换吧,把dao的数据格式在server层转化成rpc所需的数据格式。
在service和dao层传递都是dao层的数据结构。

from sniper.

flazx avatar flazx commented on May 8, 2024

@lvht 还有一个问题是,context是不是应当从server层一直传递到dao层,以让dao层可以获取到context上传递的数据?

from sniper.

taoso avatar taoso commented on May 8, 2024

dao 层的参数在在 dao 层定义,可以在 service 层组装。

ctx 需要一路传递,跟代码分层没有关系。

from sniper.

flazx avatar flazx commented on May 8, 2024

@lvht 感谢回复

from sniper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.