Created
March 2, 2026 19:27
-
-
Save wullemsb/69731133de8e7265b23edefa8052ef91 to your computer and use it in GitHub Desktop.
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
| /// <summary> | |
| /// Filter provider for Unity. | |
| /// </summary> | |
| public class UnityFilterAttributeFilterProvider : FilterAttributeFilterProvider | |
| { | |
| private readonly IDependencyResolver _resolver; | |
| /// <summary> | |
| /// Initializes a new instance of the <see cref="UnityFilterAttributeFilterProvider"/> class. | |
| /// </summary> | |
| /// <remarks> | |
| /// The <c>false</c> constructor parameter passed to base here ensures that attribute instances are not cached. | |
| /// </remarks> | |
| public UnityFilterAttributeFilterProvider(IDependencyResolver resolver) | |
| : base(false) | |
| { | |
| _resolver = resolver; | |
| } | |
| /// <summary> | |
| /// Aggregates the filters from all of the filter providers into one collection. | |
| /// </summary> | |
| /// <param name="controllerContext">The controller context.</param> | |
| /// <param name="actionDescriptor">The action descriptor.</param> | |
| /// <returns> | |
| /// The collection filters from all of the filter providers with properties injected. | |
| /// </returns> | |
| public override IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor) | |
| { | |
| var container = ExtractContainer(_resolver); | |
| var filters = base.GetFilters(controllerContext, actionDescriptor).ToArray(); | |
| foreach (var filter in filters) | |
| { | |
| container.BuildUp(filter.Instance.GetType(), filter.Instance); | |
| } | |
| return filters; | |
| } | |
| private IUnityContainer ExtractContainer(IDependencyResolver _resolver) | |
| { | |
| var unityResolver = _resolver as UnityDependencyResolver; | |
| return unityResolver.ChildContainer; | |
| } | |
| } | |
| } |
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
| /// <summary> | |
| /// Collection with global filters. | |
| /// </summary> | |
| public class UnityGlobalFilterCollection | |
| { | |
| private IList<Type> _globalFilters; | |
| /// <summary> | |
| /// Initializes a new instance of the <see cref="UnityGlobalFilterCollection"/> class. | |
| /// </summary> | |
| public UnityGlobalFilterCollection() | |
| { | |
| _globalFilters = new List<Type>(); | |
| } | |
| /// <summary> | |
| /// Adds the specified type. | |
| /// </summary> | |
| /// <param name="type">The type.</param> | |
| public void Add(Type type) | |
| { | |
| _globalFilters.Add(type); | |
| } | |
| /// <summary> | |
| /// Resolves the filter items from the container. | |
| /// </summary> | |
| /// <param name="container">The container.</param> | |
| /// <returns></returns> | |
| public IEnumerable<Filter> ResolveItemsFromContainer(IUnityContainer container) | |
| { | |
| foreach (var filter in _globalFilters) | |
| { | |
| yield return new Filter(container.Resolve(filter), FilterScope.Global,_globalFilters.IndexOf(filter)); | |
| } | |
| } | |
| } |
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 UnityGlobalFilterProvider: IFilterProvider | |
| { | |
| private readonly IDependencyResolver _resolver; | |
| /// <summary> | |
| /// Initializes a new instance of the <see cref="UnityGlobalFilterProvider"/> class. | |
| /// </summary> | |
| /// <remarks> | |
| /// The <c>false</c> constructor parameter passed to base here ensures that attribute instances are not cached. | |
| /// </remarks> | |
| public UnityGlobalFilterProvider(IDependencyResolver resolver) | |
| { | |
| _resolver = resolver; | |
| } | |
| /// <summary> | |
| /// Aggregates the filters from all of the filter providers into one collection. | |
| /// </summary> | |
| /// <param name="controllerContext">The controller context.</param> | |
| /// <param name="actionDescriptor">The action descriptor.</param> | |
| /// <returns> | |
| /// The collection filters from all of the filter providers with properties injected. | |
| /// </returns> | |
| public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor) | |
| { | |
| var container=ExtractContainer(_resolver); | |
| return Filters.ResolveItemsFromContainer(container); | |
| } | |
| private IUnityContainer ExtractContainer(IDependencyResolver _resolver) | |
| { | |
| var unityResolver = _resolver as UnityDependencyResolver; | |
| return unityResolver.ChildContainer; | |
| } | |
| /// <summary> | |
| /// Gets or sets the filters. | |
| /// </summary> | |
| /// <value> | |
| /// The filters. | |
| /// </value> | |
| [Dependency] | |
| public UnityGlobalFilterCollection Filters { get; set; } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment