问题
When applying the MVP pattern to ASP.NET applications, where does using AJAX to post data fit? Of what I know of the MVP pattern, the UI is simply that(appearance), and all the heavy lifting is done in the presenter. I don't see how you could follow the pattern and still use AJAX interacting with web services on the client. Does anyone have any references as to how one can use AJAX and web services and still follow the MVP pattern?
Thanks!
回答1:
I use the same approach with AJAX as with a regular ASP.Net post back. The view receives the post but passes it to the presenter that handles the processing. In the view I just parse the request coming from AJAX and then invoke the appropriate method on the Presenter class.
A web service can be considered as a view that "renders" a message instead of rendering HTML. I treat the web service as the view and use a presenter the same way as with an ASPX page, with the added advantage that I don't need to parse the request.
I decouple the presenter from the message leaving for the web service the responsibility of creating the return message from the view data assigned by the presenter. The input message is also known only at the web service level.
来源:https://stackoverflow.com/questions/732837/asp-net-mvp-and-ajax-posting-webservices