Temp service provider implementation

Well, Bill Pierce in his post about making the HttpContext wrapper use the application as a service provider got me to a good enough solution for the script service I had wanted in my previous post.

While not quite as clean as adding it to the HttpContext, I think an extension method on HttpApplication like this will work well for now.

public static object GetService(this HttpApplication application,Type service)
{
    IServiceProvider provider = application as IServiceProvider;
    if (provider == null)
        return null;

    return provider.GetService(service);
}

Then add/implement the IServiceProvider on the Global class. Allowing me to get an instance of the script service using 

IScriptService scriptSvc 
    = this.HttpContext.ApplicationInstance.GetService(typeof(IScriptService)) as IScriptService;

Though though the more I think of it, the more likely I am to stick this method on the Ajax and Html helper classes. I wonder how hard it would be to add an interface like IMvcHelper to both the AjaxHelper and HtmlHelper Classes my extension method would be available to both, without duplicating code.

Print | posted on Tuesday, December 18, 2007 11:06 AM

Feedback

No comments posted yet.

Your comment:





 
Please add 2 and 4 and type the answer here:

Copyright © Sean Lynch

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski