Closed bsohi closed 11 years ago
Could please describe the problem a bit more with some pseudo-code.
Thanks
Am 27.08.2012 um 15:34 schrieb bsohi notifications@github.com:
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.
//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
Edited comments from bsohi, still needs review
Just wondering, any planning to fix this issues??
thx Balwinder
Added in 3.0.2
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