views:

635

answers:

3

How do I configure Castle Windsor to use an Initialize method for a static helper class when requesting an object? I am trying to add some extension methods HtmlHelper so it has to be a static class and method. My HtmlHelper extensions depend on a IHtmlHelpersService that is configured with Castle Windsor already. I am using Convention Over Configuration.

Edit I think what I want is a ComponentActivator or the UsingFactory. Am I going down a dead end that way?

+1  A: 

I think you mean Convention over configuration?

I don't know of any way to do what you suggest. It's a bit hacky, but you could always create a class that is not static that conforms to the interface and simply executes the static methods you need it to. Sometimes this type of thing is a necessary evil when using a third-party project. Of course if you contributed the patch for static support to the Castle project, they may accept it

slf
Doh, yea. I fixed it.
Daniel A. White
A: 

I don't think IoC can help you with static classes, Windsor (or any other IoC for that matter) need something it can instantiate (call a constructor).

Are you trying to swap out helper methods depending on configuration? If so, maybe you could write a single set of extension methods that call the IoC for their implementations.

Chris Brandsma
A: 

Another option, is to create a base class for all of your ViewPages. Hide the existing HtmlHelper property with your own version:

public class MyHtmlHelper : HtmlHelper
{
  // Take helper service, implement special methods
}

public class MyViewPage : ViewPage
{
  protected new MyHtmlHelper HtmlHelper { get; set; }
  // Wire up HtmlHelper as appropriate -- which is not going to be easy since
  // the base property could be set at any time.
}

Now all your view pages will use your special MyHtmlHelper with all your special sauce, and they don't even need to be extension methods. A variation is to have your MyHtmlHelper expose the service and continue to write extension methods that target that helper.

Talljoe