Ideas on Global Filter Providers in MVC 3

by 23. July 2010 02:15

Today I enjoyed a session (technically i'm still listening) by Phil Haack of Microsoft at the Virtual MVC conference discussing MVC 3. He discussed Global Filter providers which will enable you to apply a filter attribute to a number of controllers in pretty much a single line of code. He also showed Conditional Filters which can be used to conditionally apply a filter based on some code check (his example was a check as to whether we were in debug mode).

I have been working with filters today and it solved something i was thinking about today. I like the idea of extending it and i suspect that (and i don't know any more about them than what i saw on the slides) conditional filters *may* solve a number of things i'd like to be able to do.

So I asked a question about using RegEx and patterns with the filters. What I mean by that is being able to read the metadata of the controller classes and methods and being able to apply filters conditionally to them.

For example:

 

  • Apply a trace filter to an methods marked with [HttpPost]
  • Apply a debug filter whenever a method in the "AccountController" or "ProfileContoller" is called.
  • ... even apply a debug filter whenever a method called "Save" is called in the "AccountController" or "ProfileContoller" is called.
These are some basic examples, but with the ability to apply global filters combined with an ability to read metadata on controllers, methods and so on (and perhaps even the state of the user or application) you can imagine a host of neat filters that could be quickly added to your code.

Further, being able to specify these rules declaratively and using a declarative service locator technique to accomplish DI for your filters could mean it would be super easy to attach filters to your applications without recompiling.

 

Tags: , , ,

tech

Programmatic Screen Navigation in SketchFlow

by 8. May 2010 23:32

If you want to navigate between screens in SketchFlow you can put this class at the top level in your namespace and it will do the bulk of the work for you. The code also details the meaning of the parameters you need to pass in to make it work.

/// <summary>
/// A global navigator instance that allows programmatic 
/// navigation between screens in SketchFlow.
/// </summary>
public static class Navigator
{
    /// <summary>
    /// Adds a navigation option to the global navigator.
    /// </summary>
    /// <param name="target">The namespace qualified name of the page we are navigating to.</param>
    /// <param name="eventname">The name of the event to watch for in the "obj" parameter</param>
    /// <param name="obj">The object, such as a button.</param>
    public static void Add(string target, string eventname, DependencyObject obj)
    {
        // Create a navigation instance of the screen we are navigating to
        Microsoft.Expression.Prototyping.Behavior.NavigateToScreenAction navigateAction 
            = new Microsoft.Expression.Prototyping.Behavior.NavigateToScreenAction();
        navigateAction.TargetScreen = target;
        
        // create a trigger to fire the action
        System.Windows.Interactivity.EventTrigger clickTrigger 
            = new System.Windows.Interactivity.EventTrigger(eventname);
        clickTrigger.Actions.Add(navigateAction);            
        
        // associate the trigger with the object
        System.Windows.Interactivity.TriggerCollection triggerCollection 
            = System.Windows.Interactivity.Interaction.GetTriggers(obj);
        triggerCollection.Add(clickTrigger);            
    }
}

To use it, you can put this after InitializeComponent(); in the class on the screen you are invoking the action from.
Navigator.Add("MyScreens.Screen_1_4", "Click", this.MyButton);

Tags: , , ,

tech