In order to flush changes to RavenDB in ASP.NET WebApi I created the following Action Filter:
public class RavenDbUnitOfWorkAttribute : ActionFilterAttribute
{
public Func<IDocumentSession> SessionFactory { get; set; }
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var session = SessionFactory.Invoke();
if (session != null && actionExecutedContext.Exception == null)
{
session.SaveChanges();
}
base.OnActionExecuted(actionExecutedContext);
}
}
To inject the IDocumentSession I created a custom IFilterProvider that loops through each filter and sets any dependencies using setter injection (StructureMap).
I would like to know how instances of IFilterProvider are scoped. Reading this article it appears Controllers are created per request.
Currently I'm explicitly scoping the IDocumentSession per request using StructureMap. The question is, if I instead rely on the IDependencyScope (that uses a nested container) would the same instance of IDocumentSession be injected into my Action Filter?
[Update]
Testing further it would seem that Action Filters do not use the same dependency scope as a Controller. I'd prefer not to have the code for flushing the session in my Controller however.