Skip to content

Instantly share code, notes, and snippets.

@ColinScott
Created November 24, 2012 05:16
Show Gist options
  • Save ColinScott/4138537 to your computer and use it in GitHub Desktop.
Save ColinScott/4138537 to your computer and use it in GitHub Desktop.
Providing Context with interfaces and ASP.NET MVC Filter Providers
public class FooActionFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var fooController = (IFoo) filterContext.Controller;
fooController.Context = new FooContext();
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
var fooController = (IFoo)filterContext.Controller;
fooController.Context.Dispose();
}
}
public class FooFilterProvider : IFilterProvider
{
public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
if (!(controllerContext.Controller is IFoo))
{
yield break;
}
yield return new Filter(new FooActionFilter(), FilterScope.Global, 1);
}
}
public interface IFoo
{
IFooContext Context { get; set; }
}
public interface IFooContext : IDisposable
{
void DoFoo();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment