Filters are executed before and|or after your actions. It is useful for security, dynamic content and to keep away repetitive code.Keep away repetitive code is what I am tracing, let the hear the story shall we?
This happen with one of the real world project that I am working on Data Bridge like any other web application it need authentication and passing some user preference along each action (inherit from SecureController) to the view such as List, Add, Edit & Delete action & view.
This can be done using the following technique:
Create Filters\AuthenticationFilter.cs
public class AuthenticationFilter: IFilterCreate SecureController
{
bool IFilter.Perform(ExecuteEnum exec,
IRailsEngineContext context,
IController controller){
if(!UserNotLogin())
{
Redirect("Login", "Index");
}
else
{
controller.PropertyBag["maxRow"] = GetUserMaxRowPreference();
... set more preferences as needed
}
}
}
[Layout("default"), Rescue("generalerror")]SecureController is marked as abstract, prevent from direct use.
[Filter(ExecuteEnum.BeforeAction, typeof(AuthenticationFilter))]
public abstract class SecureController : ARSmartDispatcherController
{
}
Using SecureController
public abstract class ProductController: SecureControllerNow view part: View\Product\list.vm
{
public void List()
{
}
}
When render table I want to display miximum $maxRow rows.See there is no code need to set preference in each action method, nice.