Want to follow this site? Here's the RSS feed.

Fundamentals of Themelia - Injection Processors

Friday, June 27, 2008

This documentation has been updated for Themelia Framework 2.0 Beta 4.

Another advanced HTTP routing feature up there with pre processors, mid processors, post processors, fall through processors, and post state processors is the injection processor.  These are used to automatically inject a group of HTTP handlers into Themelia. You can think of them as being packages of HTTP handlers. This comes in handy when you have a bunch of HttpHandlers and you don't really want other developers to have to register each one individually. They also come in handy when you want to think of various HTTP handlers as one cohesive unit.

For example, in Minima relies on injection processors for a lot of its supplemental functionality. Minima provides HTTP handlers for blog discovery, windows live writer, Google sitemap, MetaWeblogApi, and the core brain of Minima, namely, URL routing. However, Minima's injection processor installs each of these in one shot.

An HTTP injection processor is made by creating a class that inherits from Themelia.Web.Routing.InjectionProcessorBase. This is an abstract class that requires you to implement the following signature:

void OnAddHttpHandlers(HttpHandlerDataList injectedHandlerDataList, params Object[] parameterArray);

Within this method, you add HttpHandlerData instances to the injectedHandlerList object by using the SafelyAddHandler method.  Here is an example of adding an HTTP handler via an HttpHandlerData:

internal class CommonInjectionProcessor : InjectionProcessorBase
{
    //- @OnAddHttpHandlers -//
    public override void OnAddHttpHandlers(HttpHandlerDataList injectedHandlerDataList, params Object[] parameterArray)
    {
        SafelyAddHandler(injectedHandlerDataList, new HttpHandlerData
        {
            Name = "PassThrough",
            MatchType = "endsWith",
            MatchText = "/robots.txt"
        });
        SafelyAddHandler(injectedHandlerDataList, new HttpHandlerData
        {
            Name = "PassThrough",
            MatchType = "endsWith",
            MatchText = "/Favicon.ico"
        });
        SafelyAddHandler(injectedHandlerDataList, new HttpHandlerData
        {
            Name = "PassThrough",
            MatchType = "contains",
            MatchText = "/WebResource.axd?d="
        });
    }
}

This example is actually a snippet from Themelia's internal CommonInjectionProcessors.  Essentially, this states that robots.txt and favicon.ico should be handled directly by IIS, not by Themelia at all.

These SafelyAddHandler method calls ensures that there are no duplicates in the HTTP handler matching text. For example, if there is already an HTTP handler registered to Themelia that matches the end of a URL with the text "/robots.txt" (the key is a combination of the match type [i.e. the end of the url] and the text), this handler will not be added.  Furthermore, if everyone follows the Safely AddHandler pattern, /robots.txt will not be added again.

Also, notice that the OnAddHttpHandlers doesn't return anything. This is thanks to the nature of reference types, which allow us to modify a central reference type from many different locations. Also, the last parameter, the params array, is a more advanced topic that will not be discussed here.

Lastly, injection processors may be registered in web.config like this:

<themelia.web>
  <webDomains>
    <add>
      <injectionProcessors>
        <add type="Sample.Web.Routing.InjectionProcessor, Sample.Web" />
      </injectionProcessors>
    </add>
  </webDomains>
</themelia.web>

Links

Creative Commons License
This work is licensed under a Creative Commons Attribution 2.5 License.

Built on Themelia Pro 2.0

Mini-icons are part of the Silk Icons set of icons at famfamfam.com