从头开始的模型-视图-控制器架构最佳实践

发布于 2024-12-07 10:49:47 字数 224 浏览 1 评论 0原文

我需要了解MVC架构实现的最佳实践。我是一名 Java 和 C# 程序员。我了解 MVC 的基础知识。但我对如何实现它有点困惑。我知道如何制作一个简单的基于 MVC 的计算器。但事情是这样的。

我想使用 MVC 制作一个简单的数据库编辑器应用程序。我应该为表的每一行(对象)构建一个模型和控制器吗?如果是这样,每个对象的视图怎么样?我如何处理它们被删除、更新和插入。我应该为编辑器制作模型和控制器(这也是一个视图)吗?

I need to understand the best practices of MVC architecture implementation. I'm a Java and C# programmer. I know the basic of MVC. But i'm sorta confused on how to implement it. I know how to make a simple MVC based calculator. But here's the thing.

I wanted to make a simple database editor application using MVC. Should I construct a model and controller for every rows (objects) of the table? If so, how about the view of every objects? How do i handle them being deleted, updated, and inserted. And should I make the model and controller for the editor which is also a view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌若浮生 2024-12-14 10:49:47

如果您不想使用 Java Persistence API,请考虑使用 类文字作为运行时类型令牌 TableModel

If you don't want to use the Java Persistence API, consider using a Class Literals as Runtime-Type Token in your TableModel.

踏雪无痕 2024-12-14 10:49:47

首先,如果您熟悉 Java,请尝试 Spring MVC。有很多关于这方面的教程。如果您对 C# 更有信心,请尝试 ASP .NET MVC 3。我更喜欢后者,因为在这种情况下您必须处理更少的配置。

现在我就一一回答你的问题。

首先为数据库中的每个表创建一个模型。实际上,这些模型(只不过是类)在实例化时只不过是相应表的一行。您的 ORM(对象关系映射)工具(对于 java,您可以使用 hibernate,对于 c#.net,您可以使用实体框架)将为您提供特定的方法(save(object)、add(object)、delete(object))来更新 。

现在每个控制器应该与特定的模型一起工作(这里我忽略使用多个模型的复杂性。) 但它可能会产生很多观点。通过单击视图页面中的链接,您实际上调用了控制器中的相关方法。然后,控制器将数据(如果有)与与该链接相关的特定视图绑定,然后呈现该视图。因此,为了删除一行,控制器中应该有一个名为 delete() 的方法(您可以将其命名为任何您想要的名称,所以不要混淆)。当您想要删除一行时,调用该方法,并在该方法内使用诸如 delete(object) 之类的方法删除该行(这些方法将由您的 ORM 提供),然后返回另一个视图。同样的事情也适用于添加和更新数据。但每种方法可能会产生不同的观点。在这些方法中返回哪个视图取决于您。

希望我的回答对您有所帮助。干杯!

At first, if you are comfortable with Java try the Spring MVC. There are a lot of tutorial regarding this. If you are much more confident in C# try ASP .NET MVC 3. I will prefer the later one as in this case you have to deal with less configuration.

Now I will answer your question one by one.

At first create a model for every table in your database. Actually these models (which are nothing but classes) when instantiated are nothing but an individual row of the respective table. Your ORM (object relational mapping) tool (For java you can use hibernate, for c#.net you can use entity framework) will provide you specific methods (save(object), add(object), delete(object)) for updating the database

Now each controller should work with a specific model (Here I am ignoring the complexities of using multiple models.). But it may generate numerous views. By clicking a link in your view page you actually invoke the related method in the controller. The controller than binds the Data (if any) with the specific view realted to that link and then the view is rendered. So for deleting a row there should be a method named delete() (you may name it anything you want, so dont be confused) in your controller. When you want to delete a row invoke that method and inside the method remove that row by using something like delete(object) (these methods will be provided by your ORM) and then return another view. The same thing is applied for adding and updating data. But each method may generate different views. Its upto you that which view you return in each of these methods.

I hope the answer helps you. Cheers !!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文