Where should my SQL code go in MVC

huangapple go评论107阅读模式
英文:

Where should my SQL code go in MVC

问题

我刚开始学习MVC设计模式,我想知道我的SQL代码应该放在哪里。

例如,假设我有一个注册表单的结构,如下所示:

type Form struct {
 Username string
 Password string
}

我假设表单结构是模型的一部分,所以我有一个与表单相关的函数,在用户提交表单后,数据被放入数据库中,所以我的函数看起来像这样:

func (f *Form) registerUser() {
   // SQL代码放在这里
}

这样做是最好的方式吗?我一直在寻找使用MVC模式的开源Golang Web应用程序,但我还没有找到一个我完全理解的。

英文:

I'm just starting to learn the MVC design pattern, and I was wondering where should my SQL code go.

For example, lets say I have a register form structure that looks like this

type Form struct {
 Username string
 Password string
}

I assume that the form structure is part of the model, so I have some function that goes with the form that after a user submits the form, the data gets put into the database, so my function would look something like this

func (f *Form) registerUser() {
   // SQL code goes here
}

Is this the best way of doing it? I have been searching around for open source Golang web apps that utilizes the MVC pattern, but I have not be able to find one which I completely understand.

答案1

得分: 3

在模型-视图-控制器(Model-View-Controller)模式中...

模型(Model)用于表示你的所有类所代表的现实世界对象。

视图(View)是表单和用户可以看到和交互的所有图形元素。

控制器(Controller)用于控制类,是程序的所有逻辑部分。对于你提到的 SQL 代码,你可以实现一个数据访问对象(DAO)模式,将所有的 SQL 代码放在控制器包中,并将数据库类放在实体包中(我将其放在控制器类中)。

英文:

In model-view-controller pattern...

Model is for entities all your classes represent real world objects.

View is the forms and all the graphic things user can see and interact with.

Controller is for controller classes, is all the logic of the program, for the sql code as you said you can implement a dao pattern and have all the sql code in the controller package and the database class in the entities package(i leave it in the Controller class).

答案2

得分: 0

我假设表单结构是模型的一部分,所以我有一些与表单相关的函数,当用户提交表单时,数据会被放入数据库中,所以我的函数大致如下:

在MVC应用程序架构中,模型的另一个用途是存储可重用的代码。因此,是的,你可以将表单存储在模型中(例如,如果你在多个视图中重用它),但这比将表单存储在视图中并在以后重用它更没有意义。

执行的回溯过程如下:

  1. 控制器处理请求 - 在这里我个人会处理业务逻辑,还会(如果需要的话)调用...
  2. 模型处理所有来自数据库的数据检索、验证等,并将处理后的数据返回给控制器和...
  3. 视图显示相应的参数(用户数据、模板、验证结果等)。
  4. 用户填写表单并将输入提交给控制器和...
  5. 转到第1步。
英文:

> I assume that the form structure is part of the model, so I have some function that goes with the form that after a user submits the form, the data gets put into the database, so my function would look something like this

Another use of Model in MVC application architecture is to store reusable code. So, yes, you can store the form in Model (for example, if you reuse it multiple in views) but this makes less sense than storing form in a View and reuse it later on.

The execution backtrace would be something like

  1. Controller processes the request - personally, I do the business logic here and also (if necessary) invoke ...
  2. Model that handles all the data retrieval from DBMS, validation, etc. and returns processed data to Controller and ...
  3. The View is then displayed with the respective parameters (user data, template, validator result, etc.).
  4. User fills in the form and submits the input to Controller and ...
  5. GOTO 1. point

huangapple
  • 本文由 发表于 2015年4月14日 13:30:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/29619787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定