Created
November 24, 2012 05:16
-
-
Save ColinScott/4138537 to your computer and use it in GitHub Desktop.
Providing Context with interfaces and ASP.NET MVC Filter Providers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IFoo | |
{ | |
IFooContext Context { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IFooContext : IDisposable | |
{ | |
void DoFoo(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment