我们在哪里编写代码来插入或更新数据库。在Modal或控制器中 [英] where do we write code to insert or update database. In Modal or controller

查看:83
本文介绍了我们在哪里编写代码来插入或更新数据库。在Modal或控制器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触MVC arch中的编程,并且很少混淆我们在哪里编写代码来插入或更新数据库。在模态或控制器中。

提前谢谢

解决方案

我必须同意Wes Aday关于这一点并做了一些研究回来那个......



http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-4 [ ^ ]



http://www.w3schools.com/aspnet/mvc_intro.asp [ ^ ]



引用后一个站点

Quote:

Model是处理应用程序数据逻辑的应用程序的一部分。

模型对象通常从数据库中检索数据(和存储数据)。



视图是处理数据显示的应用程序的一部分。

通常视图是从模型数据创建的。



Controller是处理用户交互的应用程序的一部分。

通常,控制器从视图中读取数据,控制用户输入并发送输入数据到模型。



编辑 - 另一个来源学习MVC - 第1部分:MVC架构介绍和关注点分离 [ ^ ]

引用:

MVC没有特别提到数据访问层,因为它被理解为由模型封装。


你已经提高了一个非常好的问题和一个暧昧的问题。主要的重点是使我们的应用程序松散耦合,否则如果它紧密耦合,那么我们必须妥协许多事情。所以,最好按照

存储模式



这是什么?

现在我们有型号,这是我们的项目类。我们有控制器,但记住永远不要在控制器中使用DB操作,这很糟糕。它使紧耦合。因此,有服务方法和存储库,它们将处理所有数据库操作,即编码语言。上下文将在服务/存储库中发挥作用。

使用它们为服务接口创建构造函数在控制器中访问它们。

记住要理解的主要内容是处理Parameterless Constructor问题的依赖注入。请阅读有关依赖注入的信息。这肯定会帮助你做好编码。



谢谢。


你在Model中编写代码。模型包含类,验证和操作。 Controller与View-Model交互(在这种情况下调用你的数据库操作)。



例如,你可以看到此链接 [ ^ ]。



我写错了评论:(



感谢@ CHill60和@Wes Aday的评论。

Iam new to programming in MVC arch and little confused as to where do we write code to insert or update database. In Modal or controller.
Thank you in advance

解决方案

I have to agree with Wes Aday on this one and did some research to back that up ...

http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-4[^]

http://www.w3schools.com/aspnet/mvc_intro.asp[^]

To quote the latter site

Quote:

The Model is the part of the application that handles the logic for the application data.
Often model objects retrieve data (and store data) from a database.

The View is the parts of the application that handles the display of the data.
Most often the views are created from the model data.

The Controller is the part of the application that handles user interaction.
Typically controllers read data from a view, control user input, and send input data to the model.


Edit - another source Learning MVC - Part 1: Introduction to MVC Architecture and Separation of Concerns[^]

Quote:

MVC does not specifically mention the data access layer because it is understood to be encapsulated by the Model.


You have raised a very good question and an ambigous one. The main focus is to make our application loosely coupled, else if it is tightly coupled then we have to compromise on many things. SO, better follow the
"REPOSITORY PATTERN"

What is this??
Now We have models, that is our classes for the project. We have controllers, but remember never use DB operations in the Controllers, its bad. It makes tight coupling. So have "Service Methods" & "Repositories" they will handle all the DB operations i.e. in coding language The Context will play its role in the Services/Repositories.
Use them creating the constructors for the services interfaces in the controller and access them.
Remember a main thing here to understand is Dependency injection that handles the Parameterless Constructor issue. Please read about Dependency Injection. This will surely help you do good coding.

Thanks.


You write the code inside Model. Model contains classes, validations and operations. Controller interacts with the View-Model (in this case calls to your Database operation).

For example, you can see this link[^].

I wrote a wrong comment :(

Thanks to @CHill60 and @Wes Aday for their comments.


这篇关于我们在哪里编写代码来插入或更新数据库。在Modal或控制器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆