Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Consider patternizing concern overrides for filters #4914

Closed
rynowak opened this issue Jun 28, 2016 · 2 comments
Closed

Consider patternizing concern overrides for filters #4914

rynowak opened this issue Jun 28, 2016 · 2 comments

Comments

@rynowak
Copy link
Member

rynowak commented Jun 28, 2016

Code sample from #4506 (comment)

public interface IFooConcernFilter
{
    // Just a marker
}

public class MyGlobalFooConcernFilter : IFooConcernFilter, IActionFilter
{
    public void OnActionExecuting(...)
    {
        if (!IsClosestFooConcernFilter(context)
        {
            return;
        }
    }

    private bool IsClosestFooConcernFilter(FilterContext context)
    {
            // Determine if this instance is the 'effective' foo concern policy.
            for (var i = filters.Count - 1; i >= 0; i--)
            {
                var filter = filters[i];
                if (filter is IFooConcernFilter)
                {
                    return object.ReferenceEquals(this, filter);
                }
            }

            Debug.Fail("The current instance should be in the list of filters.");
            return false;
    }
}

public class IgnoreFooConcernAttribute : Attribute, IFooConcernFilter
{
    // Also just a marker
}

This gives [IgnoreFooConcern] overriding behavior. If you slap that on a controller or action, then the global filter knows to shut itself off.

We do this kind of thing frequently in the framework, and it's a non-obvious pattern. Maybe we should add some kind of infrastructure/extensions to make this easier for users to grok.

@Eilon Eilon added this to the Backlog milestone Jul 20, 2016
@rynowak rynowak modified the milestones: 2.1.0, Backlog Jul 18, 2017
@rynowak rynowak self-assigned this Jul 18, 2017
@rynowak
Copy link
Member Author

rynowak commented Jul 18, 2017

I'd like to update this proposal to just put the code we described above as a protected method on filter context. Removing needs design, this is a trivial amount of work.

rynowak added a commit that referenced this issue Sep 14, 2017
Adding some convenience methods to the context class and updating our
code to use them.
@rynowak
Copy link
Member Author

rynowak commented Sep 18, 2017

c09575d

SteveSandersonMS pushed a commit that referenced this issue Sep 21, 2017
Adding some convenience methods to the context class and updating our
code to use them.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants