惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - AllenDang

创业心得 - 从确定投资意向到开始发工资还有多远? 什么样的人适合创业公司? NoSql数据库使用半年后在设计上面的一些心得 Go语言实战 - 使用SendCloud群发邮件 用Go语言做产品半年的一些感觉 - AllenDang 我是怎么拿到投资的 Go语言实战 - 我需要站内搜索 Go语言实战 - revel框架教程之CSRF(跨站请求伪造)保护 Go语言实战 - revel框架教程之缓存和Job Go语言实战 - 网站性能优化第一弹“七牛云存储” - AllenDang 坚持是什么? - 作为创业者在与投资人见面之后想到的 Go语言实战 - revel框架教程之权限控制 Go语言实战 - revel框架教程之用户注册 Go语言实战 - 创业进行时之创业伊始 我为什么喜欢Go语言 用Go写Windows桌面应用 - 使用窗体设计器 使用CGO封装Windows API 用Go写Windows桌面应用 - 使用资源文件 用Go写Windows桌面应用 - 使用Form
Go语言实战 - revel框架教程之MongDB的最佳搭档revmgo
AllenDang · 2013-08-20 · via 博客园 - AllenDang

由于revel框架本身对于model层的编写没有提供任何指导,所以在设计这部分的时候就有些犹豫,反复斟酌到底怎样才算是最佳实践。

我在做山坡网的时候刚开始也纠结了一下,拿不准mongodb的session的创建和销毁应该在什么地方处理。直到有一天看到了revmgo的作者在与revel的作者讨论(具体内容在这里),就去研究了下revmgo,之后立即就用它替换了我自己的实现。

先说下用法吧。

1. 在app.conf添加mongodb的连接字符串。

revmgo.dial = mongodb://username:password@serverip/database

2. 在controller层的init方法中初始化。

func init() {
  revmgo.ControllerInit()

  revel.OnAppStart(func() {
    revmgo.AppInit()
  }
}

3. 把MongoController签入到所有需要访问数据库的Controller中。

type Application struct {
  *revel.Controller
  revmgo.MongoController
}

这里有个小插曲,revmgo.MongoController已经内嵌过revel.Controller了,但此处还是不能省略*revel.Controller。原因是revel在确定有效的controller的action的时候只反射了一层,跟revel的作者讨论过这个问题,后来他说服我了,原因是如果不断递归反射的话复杂度会变高而且性能损失会不可控。好吧,其实这也算是OO的遗毒。看起来挺难受,多写了一行代码,设计上也不太舒服,但仔细想来对性能和整体的复杂度却都有好处,既然如此,何必执着设计上的完美呢?简单就是美。

4. 修改Dal的实现。

//Data access layer
type Dal struct {
  session *mgo.Session
}

//创建新的Dal
func NewDal(session *mgo.Session) *Dal {
  if session == nil {
    panic("session cannot be nil.")
  }
  return &Dal{session}
}

现在来看看在controller中如何使用。

func (c *Account) HandleRegister(user *models.MockUser) revel.Result {
  //Validate parameter here.
  dal := models.NewDal(c.MongoSession)
  err := dal.RegisterUser(user)
  //Biz logic here
}

revmgo的内部实现非常简单,整个代码也就110行,基本可以一目了然。

总的来说,调用revmgo.AppInit()的时候读取app.conf中的配置,可以看到有两个配置项有效,"revmgo.dial"为连接字符串,"db.spec"确定mongodb的session的重复利用方式(具体含义参考这里),然后建立session。

之后的revmgo.ControllerInit注册了两个interceptfunc,用于在controller的action访问前后建立和关闭session。

func ControllerInit() {
  revel.InterceptMethod((*MongoController).Begin, revel.BEFORE)
  revel.InterceptMethod((*MongoController).End, revel.FINALLY)
}

其实我对于这个设计颇有微词,ControllerInit的存在简直就是对revel的module设计机制的嘲笑,很难相信作为一个模块竟然还需要使用者手动调用初始化函数。这个部分应该不难处理,常规来说只应该让使用者注册需要使用的module和执行顺序,初始化和解构都由module管理器负责。跟revel的作者也讨论了这个问题,看他似乎有别的设计意图,等等看revel 1.0正式发布时会是什么样子吧。