Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple filter of same type on Action method #25

Closed
bsohi opened this issue Aug 27, 2012 · 5 comments
Closed

Multiple filter of same type on Action method #25

bsohi opened this issue Aug 27, 2012 · 5 comments

Comments

@bsohi
Copy link

bsohi commented Aug 27, 2012

I know it is supported but we have to repeat binding for filter every time we repeat filter on action method. Is there any reason that it is not supported to bind all at once.

For example:- if we have filter "A" and we want to apply it to one of action method "B" 2 times then we need to bind it twice via ninject.

thx
Balwinder

@danielmarbach
Copy link
Member

Could please describe the problem a bit more with some pseudo-code.

Thanks

Am 27.08.2012 um 15:34 schrieb bsohi [email protected]:

I know it is supported but we have to repeat binding for filter every time we repeat filter on action method. Is there any reason that it is not supported to bind all at once.

For example:- if we have filter "A" and we want to apply it to one of action method "B" 2 times then we need to bind it twice via ninject.

thx
Balwinder


Reply to this email directly or view it on GitHub.

@bsohi
Copy link
Author

bsohi commented Aug 27, 2012

//attribute and filter
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class InjectSelectListAttribute : Attribute {

    public List<ListValueCategories> List { get; private set; }
    //Named parameter
    public bool optionalParameter { get; set; }
    public InjectSelectListAttribute(params ListValueCategories[] lists) {
        List = lists.ToList();
    }
}

public class InjectSelectListFilter : IActionFilter {
    private readonly IList<ListValueCategories> _lists;
    provate readonly bool _optionalParameter;

    public InjectSelectListFilter(List<ListValueCategories> lists, bool optionalParameter) {
        _optionalParameter = optionalParameter;
        _lists = lists;
    }

    public void OnActionExecuting(ActionExecutingContext filterContext) {
        //do logic based on "optionalParameter"
    }

    public void OnActionExecuted(ActionExecutedContext filterContext) {
    }
}

//Controller
public class LenderController : Controller{
//
    [InjectSelectList(List1)]
    [InjectSelectList(List2, optionalParameter=true)]
    public ActionResult Test(int id) {
        //do logic here
    }
}

If I do this way then i get "sequence contains no element" exception.

//Ninject binding
kernel.BindFilter<InjectSelectListFilter>(FilterScope.Action, 0)
            .WhenActionMethodHas<InjectSelectListAttribute>()
            .WithConstructorArgumentFromActionAttribute<InjectSelectListAttribute>("lists", attribute => attribute.List)
            .WithConstructorArgumentFromActionAttribute<InjectSelectListAttribute>("optionalParameter", attribute => attribute.optionalParameter);

//so instead I am doing this
for first attribute

kernel.BindFilter<InjectSelectListFilter>(FilterScope.Action, 0)
            .When((controllerContext, action) => action.GetCustomAttributes(typeof(InjectSelectListAttribute), true).Length > 0)
            .WithConstructorArgument("lists", (context, controller, action) => {
                object attr = action.GetCustomAttributes(typeof(InjectSelectListAttribute), true);
                return ((InjectSelectListAttribute)attr[0]).List;
            })
            .WithConstructorArgument("optionalParameter", (context, controller, action) => {
                object attr = action.GetCustomAttributes(typeof(InjectSelectListAttribute), true);
                return ((InjectSelectListAttribute)attr[0]).optionalParameter;
            });

for second attribute on same action

            kernel.BindFilter<InjectSelectListFilter>(FilterScope.Action, 0)
            .When((controllerContext, action) => action.GetCustomAttributes(typeof(InjectSelectListAttribute), true).Length > 1)
            .WithConstructorArgument("lists", (context, controller, action) => {
                object attr = action.GetCustomAttributes(typeof(InjectSelectListAttribute), true);
                return ((InjectSelectListAttribute)attr[1]).List;
            })
            .WithConstructorArgument("optionalParameter", (context, controller, action) => {
                object attr = action.GetCustomAttributes(typeof(InjectSelectListAttribute), true);
                return ((InjectSelectListAttribute)attr[1]).optionalParameter;
            });

Thanx

@idavis
Copy link
Member

idavis commented Aug 30, 2012

Edited comments from bsohi, still needs review

@bsohi
Copy link
Author

bsohi commented Oct 18, 2012

Just wondering, any planning to fix this issues??

thx
Balwinder

@remogloor
Copy link
Member

Added in 3.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants