/// Friday, November 25

Delaying Code Execution with Reflex

I've already found a nice benefit of using my Reflex library. I can delay the execution of an entire block of code. Here's the scenario. A user browses this address:

http://www.mgutz.org/gamba/person/show?id=1

To Gamba (Boo web framework) it means invoke Person.Show(). When the URL above is received, Gamba forwards the URL to ReflexDispatcher for dispatching. ReflexDispatcher returns a ReflexActionProxy object which may be executed at any time. Delaying execution of the action makes Gamba flexible. The action could be chained with pre and post interceptors, it could be queued, or completely discarded by a filter in the pipeline. Here' the pseudo-logic in ReflexDispatcher.

    def Execute():
        klass = ParseClass(_context)
        method = ParseMethod(_context)

        script = """
            ${klass}.new() {
                Context = $0
                ${method}()
                return this
            }
        """

        return ReflexActionProxy(script, _context)

How does it work? First remember Gamba has to parse the class and method from URL. Gamba creates a Reflex script to avoid doing any reflection manually. The script is then packaged into an action proxy along with argument $0 (_context object). The script above, after string interpolation looks like this:

    Person.new() {
        Context = $0
        Show()
        return this
    }
  1. Create Person() controller
  2. Assign the HttpContext
  3. Invoke Show()
  4. return the instanstiated Controller

To do all of this, ReflexActionProxy simply evaluates the script.

    return Reflex.Eval(script, context)

You may be wondering is Reflex using CodeDom? DEFINITELY NOT! Reflex is optimized for small, reflection-based scripts. Reflex caches types, methods and properties to minimize performance loss.

Comments:

Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?