简体   繁体   English

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

[英]model-view-controller architecture best practices from scratch

I need to understand the best practices of MVC architecture implementation. 我需要了解MVC架构实施的最佳做法。 I'm a Java and C# programmer. 我是Java和C#程序员。 I know the basic of MVC. 我知道MVC的基本知识。 But i'm sorta confused on how to implement it. 但是我对如何实现它感到困惑。 I know how to make a simple MVC based calculator. 我知道如何制作一个简单的基于MVC的计算器。 But here's the thing. 但是,这就是事情。

I wanted to make a simple database editor application using MVC. 我想使用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? 我应该为编辑器(也是视图)制作模型和控制器吗?

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

At first, if you are comfortable with Java try the Spring MVC. 首先,如果您对Java感到满意,请尝试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. 如果您对C#更有信心,请尝试使用ASP .NET MVC3。我希望使用后者,因为在这种情况下,您必须处理较少的配置。

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 您的ORM(对象关系映射)工具(对于Java,您可以使用hibernate,对于c#.net,您可以使用实体框架)将为您提供特定的方法(保存(对象),添加(对象),删除(对象))来更新数据库

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. 因此,要删除一行,在控制器中应该有一个名为delete()的方法(您可以随意命名,所以请不要混淆)。 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. 当您想删除一行时,调用该方法,并在该方法内部使用诸如delete(object)之类的方法删除该行(这些方法将由您的ORM提供),然后返回另一个视图。 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 !!! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Model-view-Controller C#将gui和模型结合在一起 - Model-view-Controller c# bring the gui and the model together 我在 Model-View-Controller 应用程序中更改列名时遇到错误 - I'm encountering an error when I change the column names in a Model-View-Controller app 根据另一个属性在视图中显示模型属性的最佳实践 - Best practices for displaying a Model Property in View depending on another Property 在MVC架构中,控制器和模型可以管理多个View吗? - In MVC architecture, can a controller and model manage more than one View? 多桌面应用程序的最佳实践和体系结构? - Best practices and architecture of an application multi desktop? Web API分层体系结构-最佳实践 - Web API tiered architecture - best practices Model 未从视图发布到 Controller - Model not posting from View to Controller 如何从头开始创建模型 - How to Create Model from Scratch 控制器vs视图模型vs域模型Asp.net MVC最佳实践VS Wicket框架MVC - controller vs viewmodel vs domain model Asp.net MVC best practices VS Wicket Framework MVC 分层体系结构中的实体框架-要遵循的最佳实践? - Entity Framework in a n tiered architecture - Best Practices to follow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM