问题描述
我有使用ASP.NET MVC 1.最近的MVC的版本包括AsyncController特点制定了博客网站。这实际上需要开发一些额外的任务。但我怎么能重用现有的code,而无需修改我的业务层。
I have a "blog" website developed using ASP.NET MVC 1. Recent version of MVC includes AsyncController feature. This actually requires some additional task in development. But how can I reuse my existing code without modifying my business layer.
在code的某些部分是这样的:
Some part of the code looks like:
BlogPost post = new BlogPost();
post.GetPost(58345);
BlogComment comments = new BlogComment();
comments.GetComments(58345);
根据当前的环境下,我需要等到请求完成两次手术。使用AsyncController,我可以同时做两个操作。但是,博文和BlogComment需要的类更改为支持异步操作,如添加事件处理器知道操作是否完成等。
As per the current environment, I need to wait till the request completes two operations. Using AsyncController, I can do two operations simultaneously. But the classes BlogPost and BlogComment requires to be changed to support for asynchronous operations like adding EventHandlers to know whether the operation is completed and etc.
我该怎么办异步操作,而无需修改现有的业务层。
How can I do asynchronous operation without modifying existing business layer.
推荐答案
您可以这样做:
public class BlogController : AsyncController
{
private readonly IBlogRepository _repository;
public BlogController(IBlogRepository repository)
{
_repository = repository;
}
public void ShowAsync(int id)
{
AsyncManager.OutstandingOperations.Increment(2);
new Thread(() =>
{
AsyncManager.Parameters["post"] = _repository.GetPost(id);
AsyncManager.OutstandingOperations.Decrement();
}).Start();
new Thread(() =>
{
AsyncManager.Parameters["comments"] = _repository.GetComments(id);
AsyncManager.OutstandingOperations.Decrement();
}).Start();
}
public ActionResult ShowCompleted(Post post, IEnumerable<Comment> comments)
{
return View(new BlogViewModel
{
Post = post,
Comments = comments,
});
}
}
您应该测量你的应用程序的性能,并决定是否引入异步控制器带来的性能的任意值。
You should measure the performance of your application and decide whether introducing an async controller brings any value to the performance.
这篇关于在ASP.NET MVC异步控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!