Sean Lynch

March 18, 2008

Temp service provider implementation

Filed under: .Net,Development,MS Mvc — Sean Lynch @ 12:55 am

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.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress