mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 23:40:35 +01:00
Update README_CN.md
Former-commit-id: 51e771a552dbb768b59c852897a8dbfddbf3c93a
This commit is contained in:
parent
064741d487
commit
4f97344c22
42
README_CN.md
42
README_CN.md
|
@ -392,30 +392,32 @@ func (c *MoviesController) DeleteBy(id int) iris.Map {
|
||||||
|
|
||||||
### MVC 快速指南 3
|
### MVC 快速指南 3
|
||||||
|
|
||||||
|
Iris是一个底层的Web开发框架,如果你喜欢按 **目录结构** 的约定方式开发,那么Iris框架对此毫无影响。
|
||||||
|
|
||||||
Nothing stops you from using your favorite **folder structure**. Iris is a low level web framework, it has got MVC first-class support but it doesn't limit your folder structure, this is your choice.
|
Nothing stops you from using your favorite **folder structure**. Iris is a low level web framework, it has got MVC first-class support but it doesn't limit your folder structure, this is your choice.
|
||||||
|
|
||||||
|
你可以根据自己的需求来创建目录结构,但是我建议你还是最好看看如下的目录结构例子:
|
||||||
|
|
||||||
|
[![目录结构例子](_examples/mvc/overview/folder_structure.png)](_examples/mvc/overview)
|
||||||
|
|
||||||
|
好了,直接上代码。
|
||||||
|
|
||||||
Structuring depends on your own needs. We can't tell you how to design your own application for sure but you're free to take a closer look to one typical example below;
|
Structuring depends on your own needs. We can't tell you how to design your own application for sure but you're free to take a closer look to one typical example below;
|
||||||
|
|
||||||
[![folder structure example](_examples/mvc/overview/folder_structure.png)](_examples/mvc/overview)
|
[![folder structure example](_examples/mvc/overview/folder_structure.png)](_examples/mvc/overview)
|
||||||
|
|
||||||
Shhh, let's spread the code itself.
|
Shhh, let's spread the code itself.
|
||||||
|
|
||||||
#### Data Model Layer
|
#### 数据模型层
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// file: datamodels/movie.go
|
// file: datamodels/movie.go
|
||||||
|
|
||||||
package datamodels
|
package datamodels
|
||||||
|
|
||||||
// Movie is our sample data structure.
|
// Movie是我们例子数据结构
|
||||||
// Keep note that the tags for public-use (for our web app)
|
// 此Movie对象可能会在"web/viewmodels/movie.go"的文件里持有
|
||||||
// should be kept in other file like "web/viewmodels/movie.go"
|
// Movie的数据模型在应用中只有一个,这样使用就很简单了
|
||||||
// which could wrap by embedding the datamodels.Movie or
|
|
||||||
// declare new fields instead butwe will use this datamodel
|
|
||||||
// as the only one Movie model in our application,
|
|
||||||
// for the shake of simplicty.
|
|
||||||
type Movie struct {
|
type Movie struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
@ -434,7 +436,7 @@ package datasource
|
||||||
|
|
||||||
import "github.com/kataras/iris/_examples/mvc/overview/datamodels"
|
import "github.com/kataras/iris/_examples/mvc/overview/datamodels"
|
||||||
|
|
||||||
// Movies is our imaginary data source.
|
// Movies是模拟的数据源
|
||||||
var Movies = map[int64]datamodels.Movie{
|
var Movies = map[int64]datamodels.Movie{
|
||||||
1: {
|
1: {
|
||||||
ID: 1,
|
ID: 1,
|
||||||
|
@ -474,9 +476,9 @@ var Movies = map[int64]datamodels.Movie{
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Repositories
|
#### 数据仓库
|
||||||
|
|
||||||
The layer which has direct access to the "datasource" and can manipulate data directly.
|
数据仓库层用来直接访问数据源
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// file: repositories/movie_repository.go
|
// file: repositories/movie_repository.go
|
||||||
|
@ -490,12 +492,10 @@ import (
|
||||||
"github.com/kataras/iris/_examples/mvc/overview/datamodels"
|
"github.com/kataras/iris/_examples/mvc/overview/datamodels"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Query represents the visitor and action queries.
|
// Query 是数据访问的集合入口
|
||||||
type Query func(datamodels.Movie) bool
|
type Query func(datamodels.Movie) bool
|
||||||
|
|
||||||
// MovieRepository handles the basic operations of a movie entity/model.
|
// MovieRepository 中会有对movie实体的基本操作
|
||||||
// It's an interface in order to be testable, i.e a memory movie repository or
|
|
||||||
// a connected to an sql database.
|
|
||||||
type MovieRepository interface {
|
type MovieRepository interface {
|
||||||
Exec(query Query, action Query, limit int, mode int) (ok bool)
|
Exec(query Query, action Query, limit int, mode int) (ok bool)
|
||||||
|
|
||||||
|
@ -506,23 +506,21 @@ type MovieRepository interface {
|
||||||
Delete(query Query, limit int) (deleted bool)
|
Delete(query Query, limit int) (deleted bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMovieRepository returns a new movie memory-based repository,
|
// NewMovieRepository 返回movie内存数据
|
||||||
// the one and only repository type in our example.
|
|
||||||
func NewMovieRepository(source map[int64]datamodels.Movie) MovieRepository {
|
func NewMovieRepository(source map[int64]datamodels.Movie) MovieRepository {
|
||||||
return &movieMemoryRepository{source: source}
|
return &movieMemoryRepository{source: source}
|
||||||
}
|
}
|
||||||
|
|
||||||
// movieMemoryRepository is a "MovieRepository"
|
// movieMemoryRepository 就是 "MovieRepository",它管理movie的内存数据
|
||||||
// which manages the movies using the memory data source (map).
|
|
||||||
type movieMemoryRepository struct {
|
type movieMemoryRepository struct {
|
||||||
source map[int64]datamodels.Movie
|
source map[int64]datamodels.Movie
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// ReadOnlyMode will RLock(read) the data .
|
// 只读模式
|
||||||
ReadOnlyMode = iota
|
ReadOnlyMode = iota
|
||||||
// ReadWriteMode will Lock(read/write) the data.
|
// 写模式
|
||||||
ReadWriteMode
|
ReadWriteMode
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user