在的UnitOfWork行动过滤器似乎缓存 [英] UnitOfWork in Action Filter seems to be caching

查看:215
本文介绍了在的UnitOfWork行动过滤器似乎缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC 3网站,使用IOC(统一),并产生我的模型瓦特/ EF4和波苏斯。我使用的是动作过滤器犯我的UnitOfWork:

I have an MVC 3 site that uses IoC (Unity), and my model is generated w/ EF4 and POCOs. I am using an action filter to commit my UnitOfWork:

public class UseUnitOfWorkAttribute : ActionFilterAttribute, IActionFilter
{
    private readonly IUnitOfWork _unitOfWork;

    public UseUnitOfWorkAttribute()
    {
        _unitOfWork = IoCFactory.Instance.CurrentContainer.Resolve<IUnitOfWork>();
    }

    void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
    {
        _unitOfWork.Commit();
    }

    void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
    {
    }
}

不过,尽管提交()似乎是被解雇,它在某种程度上似乎是缓存它认为是脏。

However, even though the Commit() seems to be getting fired, it somehow seems to be caching what it thinks is "dirty".

例如,在我的控制器,下面会从一个服务类执行的:

For example, in my controller, the following gets executed from a service class:

var user = _userRepository.Single(u => u.Id == 2);
user.DateAdded = DateTime.Now;

每当我做解决方案的一个新的构建和打击这种控制器动作,变化实际上是提交。然而,连续命中控制器没有做任何事情。

Whenever I do a fresh build of the solution and hit this controller action, the change is actually committed. However, successive hits to the controller doesn't do anything.

在另一方面,如果我把一个的UnitOfWork在我的控制器并提交下列服务方法调用,它将按预期工作(我每次请求的控制器动作):

On the other hand, if I put a UnitOfWork in my controller and commit it following the service method call, it works as expected (every time I request the controller action):

public AccountController()
{
    _unitOfWork = IoCFactory.Instance.CurrentContainer.Resolve<IUnitOfWork>();
}

public ActionResult Test()
{
    var user = _userRepository.Single(u => u.Id == 2);
    user.DateAdded = DateTime.Now;
    _unitOfWork.Commit();
}

所以它肯定看起来像某种缓存是怎么回事,但我无法弄清楚什么是越来越缓存 - 的的UnitOfWork的ActionFilter或库

So it definitely seems like some sort of caching is going on, but I can't figure it out what is getting cached -- the UnitOfWork, the ActionFilter, or the repository.

任何想法可能是怎么回事?如果没有的话,任何想法还有什么我能做的来解决?

Any ideas what could be going on? And if not, any ideas what else I could do to troubleshoot?

先谢谢了。

推荐答案

正在初始化你的工作单位在行动过滤器,这意味着当动作过滤器实例将被注入的构造函数。从 ASP.NET MVC 3发行说明报价的:

You are initializing your unit of work in the constructor of the action filter which means that it will be injected when the action filter is instantiated. Quote from the ASP.NET MVC 3 release notes:

在ASP.NET MVC的previous版本,
  行动滤膜每创建
  但在少数情况下要求。这个
  行为从来就不是一个保证
  行为而只是一种实现
  细节和过滤器合同
  是考虑他们的无状态的。在
  ASP.NET MVC 3,过滤器会缓存更多
  积极。因此,任何自定义
  行动的过滤器,存放不当
  实例状态可能被打破。

In previous versions of ASP.NET MVC, action filters were created per request except in a few cases. This behavior was never a guaranteed behavior but merely an implementation detail and the contract for filters was to consider them stateless. In ASP.NET MVC 3, filters are cached more aggressively. Therefore, any custom action filters which improperly store instance state might be broken.

这篇关于在的UnitOfWork行动过滤器似乎缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆