它如何工作每个请求会话模式?

Mar*_*ser 5 c# asp.net asp.net-mvc session design-patterns

我们正在使用ASP.NET MVC4开发一个项目.在团队的一次会议中,出现了使用Session per request 模式的想法.

我做了一些搜索,并在SO中发现了一些问题 - 一般来说 - 这种模式(如果可能被称为)它表示框架ORM.

一个小例子

//GET Controller/Test

public ActionResult Test()
{
     //open database connection

     var model = new TestViewModel 
                 {
                      Clients = _clientService.GetClients(),
                      Products = _productService.GetProducts()
                 };

     //close database connection
     return View(model);
}
Run Code Online (Sandbox Code Playgroud)

没有每个请求的会话:

//GET Controller/Test

public ActionResult Test()
{
     var model = new TestViewModel 
                 {
                      Clients = _clientService.GetClients(), // Open and close database connection
                      Products = _productService.GetProducts() // Open and close database connection.
                 };
     return View(model);
}
Run Code Online (Sandbox Code Playgroud)

疑惑

  1. 要进行上下文化,每个请求的会话如何工作?
  2. 这是一个好的解决方案吗?
  3. 实施它的最佳方法是什么?在网上打开连接?
  4. 是否在具有复杂查询/操作的项目中推荐?
  5. 在涉及交易时是否有可能出现并发问题?

Vic*_*yev 7

看起来你的意思是"每个请求的DB上下文".您可以使用工作单元模式来实现它.

您可以在本文中检查Radu Pascal的简单实现:https: //www.codeproject.com/Articles/243914/Entity-Framework-context-per-request

另一个实现(对于Entity Framework和NHibernate),您可以在ASP.NET Boilerplate中找到更复杂的内容:http://www.aspnetboilerplate.com/Pages/Documents/Unit-Of-Work