将业务服务中的同步操作更改为异步操作存储库层 (ASP.NET MVC 2)

发布于 2024-11-03 08:39:52 字数 1479 浏览 1 评论 0原文

我正在开发一个与旅行相关的网站,这是我的高级架构:

ASP.NET MVC 2 (Presentation Layer)  
Services Layer  
Repository Layer communicating with external J2EE Services

目前我的所有层仅支持这样的同步通信:

//Pricing Controller...
public ActionResult GetRates()
{
    //Contruct Pricing Request            
    //PricingRequest request = Contruct Pricing Request...; 
    PricingResponse response = _pricingService.GetComponentPricing(request);
    //....
    //...
    return View(response);
}

//Pricing Service...
public PricingResponse GetComponentPricing(PricingRequest pricingRequest)
{
    //Do Something...
    PricingResponseDomainObject responseDomainObject = _pricingRepository.GetComponentPricing(pricingRequest.ConvertToPricingRequestDomainObject());
    //Apply Business Rules and perform some action on response
    //Convert DomainObjectResponse into ServiceObjectResponse...
    return response(PricingResponse);
}

//Pricing Repository...
public PricingResponseDomainObject GetComponentPricing(PricingRequestDomainObject pricingRequest)
{
    //Do Something...
    //Call J2EE Service and Get Response
    //Return Response
    return response(PricingResponseDomainObject);
}

现在我需要将我的定价控制器更改为 AsyncController 并转换 public ActionResult GetRates() 进入异步操作。

我需要在我的服务和服务中进行哪些更改?存储库层为了支持异步操作?我必须完全重写它们吗?

编辑:需要将控制器更改为异步

对于特定的搜索场景,我需要调用 J2EE 服务 1 到 5 次(独立操作),将所有响应合并为单个响应并将其交给控制器能够将其呈现给用户。

I am working on a Travel related website and here is my high level architecture:

ASP.NET MVC 2 (Presentation Layer)  
Services Layer  
Repository Layer communicating with external J2EE Services

Currently all of my layers support ONLY Synchronous communication like this:

//Pricing Controller...
public ActionResult GetRates()
{
    //Contruct Pricing Request            
    //PricingRequest request = Contruct Pricing Request...; 
    PricingResponse response = _pricingService.GetComponentPricing(request);
    //....
    //...
    return View(response);
}

//Pricing Service...
public PricingResponse GetComponentPricing(PricingRequest pricingRequest)
{
    //Do Something...
    PricingResponseDomainObject responseDomainObject = _pricingRepository.GetComponentPricing(pricingRequest.ConvertToPricingRequestDomainObject());
    //Apply Business Rules and perform some action on response
    //Convert DomainObjectResponse into ServiceObjectResponse...
    return response(PricingResponse);
}

//Pricing Repository...
public PricingResponseDomainObject GetComponentPricing(PricingRequestDomainObject pricingRequest)
{
    //Do Something...
    //Call J2EE Service and Get Response
    //Return Response
    return response(PricingResponseDomainObject);
}

Now I have a need to change my Pricing Controller to AsyncController and convert public ActionResult GetRates() into Asynchronous Action.

What changes do I need to do in my Service & Repository layers in order to support Asynchronous operations? Do I have to re-write them completely?

Edit: Need for changing Controller into Asynchronous

For a particular Search scenario, I need to call the J2EE service 1 to 5 times (independent operations), consolidate all responses into a single response and hand it over to the Controller to be able to present it to user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

書生途 2024-11-10 08:39:52

不,您可能不需要做任何事情。事实上,将它们本身转换为异步会使您的代码变得更加复杂。异步控制器背后的想法是,控制器操作在完成处理后返回,并且由于这些方法仍然是同步的,所以效果很好。你做你的工作,完成后返回。

No, you likely will not have to do anything. In fact, converting them to async themselves would make your code a lot more complicated. The idea behind an async controller is that the controller action returns when it's done processing, and since those methods will still be syncronous that works out just fine. You do your work, and return when you're finished.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文