In client side MVC, who should handle client-server communication?

雨燕双飞 提交于 2019-12-24 06:14:15

问题


I'm working on a client/server product. Basically, server will transfer a document to client side to do editing. The client side has full MVC architecture. The document is the model.

Now the problem are:

  1. There are some calculation in the model that need some resources in server.
  2. For performance reason, some part of the model should be lazy loaded.

One example is the image in a document. It didn't load when opening the document, but there is something that load the image, once it loaded it will let the document know and document will recalculate the layout.

My question is if the communication code is part of Model or Controller? Or it belongs to some Context that is neither Model nor Controller? Or the Context belongs to Model?


回答1:


The model layer should be interacting with data source. In case of client-server setup where you have two separate and independent triads, the data source for client's model layer would be server's presentation layer.

Basically, your client-side's model layer becomes the user of server-side.




回答2:


It will be better if you can provide some calculation example or document object model.

Let's break through the requirement:

  1. There are some calculation in the model that need some resources in server.

    This kind of calculation is better to be put at Model, because it needs resources from server. If you are put the logic at Controller, then:

    • The Controller need access to server (database), in which break the MVC rule. Another thing is, the Controller now know the connection (either string or physical file storage). If you add another adapter / bridge, then it is additional effort
    • The calculation cannot be applied to other UI-implementation. Say in .Net, you put it in Asp.Net MVC and add the calculation at Controller. If sometimes you need to support desktop UI, then the calculation cannot be used as is (because already being tainted with controller actions, added useless web dependency, etc)
  2. For performance reason, some part of the model should be lazy loaded.

    I'm not sure about your objective with this. But let we go through this. I'm assuming that you has Header model which has List of Details that need to be lazy loaded. This can be achieved with 2 approach.

    First approach is to implement lazy load at the Details property, and second approach is to retrieve a list of Details given by specific Header or id, retrieved from the repository. Both of them are resulting the same. IMHO I like the second better, because with later solution, you can reuse the repository in other module, and enable you to select Details without specific Header. The placement, I believe it should be on Model.

I may misunderstand the requirement though.



来源:https://stackoverflow.com/questions/16907874/in-client-side-mvc-who-should-handle-client-server-communication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!