开发者

model-view-controller architecture best practices from scratch

开发者 https://www.devze.com 2023-04-10 08:52 出处:网络
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 开发者_C百科on how to implement it. I know h

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 开发者_C百科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?


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


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 !!!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号