google-code-export / sutekishop

Automatically exported from code.google.com/p/sutekishop
1 stars 1 forks source link

Filter states #33

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
There is problem with filters, used in sutekishop.
According to msdn: "Do not store filter state in a filter instance. Per-request 
state would typically be stored in the Items() property. This collection is 
initialized to empty for every request and is disposed when the request 
completes. Transient per-user state is typically stored in the user's session. 
User-state information is often stored in a database. The downloadable sample 
for this topic includes a timing filter that uses per-request state."

It was not a problem in MVC 2, but in MVC 3 it becomes a pain, because filters 
are cached and not created for each request.

So for example unitofwork is 

public class UnitOfWorkFilter : IActionFilter
    {
        private readonly IDataContextProvider provider;

        public UnitOfWorkFilter(IDataContextProvider provider)
        {
            this.provider = provider;
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var context = provider.DataContext;

            if (filterContext.Controller.ViewData.ModelState.IsValid)
            {
                context.SubmitChanges();
            }
        }
    }

and as you can see, it has a state - provider. And as long as it is not 
recreated for each request - it gives "object is disposed" exception. 
Looks like such implementation would solve a problem:
 public class UnitOfWorkFilter : IActionFilter
    {       
        public UnitOfWorkFilter()
        {            
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var provider = ServiceLocator.Current.GetInstance<IDataContextProvider>();
            filterContext.HttpContext.Items.Add(filterContext.ActionDescriptor, provider);
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var provider = (IDataContextProvider)filterContext.HttpContext.Items[filterContext.ActionDescriptor];
            var context = provider.DataContext;

            if (filterContext.Controller.ViewData.ModelState.IsValid)
            {
                context.SubmitChanges();
            }
        }
    }

Original issue reported on code.google.com by Giedrius...@gmail.com on 28 Jan 2011 at 8:29

GoogleCodeExporter commented 9 years ago
Thanks Giedrius, I'm planning to move Suteki Shop to MVC 3 next week, so this 
is very useful information.
Mike

Original comment by mikehad...@googlemail.com on 28 Jan 2011 at 9:07

GoogleCodeExporter commented 9 years ago
This is only a problem with much older Linq-to-SQL versions of the codebase. 
The new NH version is not affected.

Original comment by mikehad...@googlemail.com on 7 Feb 2011 at 11:28