Created
December 31, 2013 14:55
-
-
Save ielcoro/8197981 to your computer and use it in GitHub Desktop.
Asp.Net Filtering Inyection
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
| container.RegisterGlobalActionFilter<MvcTestFilterAttribute>(); |
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
| GlobalFilters.Filters.Add(new ActionFilterWrapperAttribute(typeof(MvcTestFilterAttribute))); | |
| [ActionFilterWrapper(typeof(MvcTestFilterAttribute))] | |
| // | |
| // GET: /Mvc2/ | |
| public ActionResult Index() | |
| { | |
| return View(); | |
| } |
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 static class UnityContainerMvcExtensions | |
| { | |
| public static void RegisterGlobalActionFilter<T>(this IUnityContainer container) | |
| where T : IActionFilter | |
| { | |
| container.RegisterType<T>(new PerRequestLifetimeManager()); | |
| GlobalFilters.Filters.Add(new ActionFilterWrapperAttribute(typeof(T))); | |
| } | |
| } |
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 static class UnityWebActivator | |
| { | |
| /// <summary>Integrates Unity when the application starts.</summary> | |
| public static void Start() | |
| { | |
| var container = UnityConfig.GetConfiguredContainer(); | |
| FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First()); | |
| FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container)); | |
| DependencyResolver.SetResolver(new UnityDependencyResolver(container)); | |
| Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule)); | |
| } | |
| } |
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
| GlobalFilters.Add(new SimpleMvcFilterAttribute()); |
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
| GlobalFilters.Add(new FilterWithCollaborators(new LoggingCollaborator(), new UnitofWork())); |
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 static class UnityContainerWebApiExtensions | |
| { | |
| public static void RegisterGlobalActionFilter<T>(this IUnityContainer container) | |
| where T : IActionFilter | |
| { | |
| container.RegisterType<T>(new PerRequestLifetimeManager()); | |
| GlobalConfiguration.Configuration.Filters.Add(new ActionFilterWrapperAttribute(typeof(T))); | |
| } | |
| } |
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 static class UnityWebApiActivator | |
| { | |
| /// <summary>Integrates Unity when the application starts.</summary> | |
| public static void Start() | |
| { | |
| var resolver = new UnityHierarchicalDependencyResolver(UnityConfig.GetConfiguredContainer()); | |
| var defaultProviders = GlobalConfiguration.Configuration.Services.GetFilterProviders(); | |
| GlobalConfiguration.Configuration.Services.Remove(typeof(IFilterProvider), defaultProviders.First(f => f is ActionDescriptorFilterProvider)); | |
| GlobalConfiguration.Configuration.Services.Add(typeof(IFilterProvider), new WebApiUnityFilterProvider(UnityConfig.GetConfiguredContainer())); | |
| GlobalConfiguration.Configuration.DependencyResolver = resolver; | |
| } | |
| } |
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 WebApiUnityFilterProvider : ActionDescriptorFilterProvider, IFilterProvider | |
| { | |
| private readonly IUnityContainer container; | |
| public WebApiUnityFilterProvider(IUnityContainer container) | |
| { | |
| this.container = container; | |
| } | |
| public new IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor) | |
| { | |
| var filters = base.GetFilters(configuration, actionDescriptor); | |
| List<FilterInfo> filterInfoList = new List<FilterInfo>(); | |
| foreach (var filter in filters) | |
| { | |
| container.BuildUp(filter.Instance.GetType(), filter.Instance); | |
| } | |
| return filters; | |
| } | |
| } |
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
| internal class ActionFilterWrapperAttribute : FilterAttribute, IActionFilter | |
| { | |
| private readonly Type filter; | |
| public ActionFilterWrapperAttribute(Type filter) | |
| { | |
| this.filter = filter; | |
| } | |
| public void OnActionExecuted(ActionExecutedContext filterContext) | |
| { | |
| IActionFilter filterInstance = (IActionFilter)DependencyResolver.Current.GetService(filter); | |
| filterInstance.OnActionExecuted(filterContext); | |
| } | |
| public void OnActionExecuting(ActionExecutingContext filterContext) | |
| { | |
| IActionFilter filterInstance = (IActionFilter)DependencyResolver.Current.GetService(filter); | |
| filterInstance.OnActionExecuting(filterContext); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment