I'm using CastleWindsor as my dependency injection framework and it all works well when you're in a Controller, because we can make use of constructor injection with the controllerfactory.
But there are some specific cases that dependency injection ( constructor injection ) would not work.. For example: I want to be able to resolve my dependencies still with IOC in some utility classes or extension methods ( eg. of HtmlHelper ) in my Views. I know that some people wouldn't agree on that and rather keep the views dumb, but let's keep that out the discussion.
So this basically leaves me with one option and that's using... the service locator. So I know that the Service Locator is considered by most people as an anti pattern and I do understand why.. But how do you resolve your dependencies with IOC if you can't use dependency injection? For all that I know is that it's still better to use the service locator for IOC than having nothing. I would like to avoid the service locator pattern, but I do not seem to understand how to avoid it in some specific cases.
Next question is.. so even if you like/dislike the service locator. Which is the best option to implement this with CastleWindsor?
So I guess the options would be:
Expose the container as a global object ( or through some other global object that wraps the container ) that you can retrieve from anywhere in your code. You can then just call the resolve and release methods on the container. One thing I don't like about this is, that we have to call release explicitly for transient lifestyle objects. If an inexperience developer does not do this, you'd end up having a memory leak.
I also found: https://www.nuget.org/packages/CommonServiceLocator.WindsorAdapter and it has a lot of downloads.. ( Same principal as option 1 but more generic and wraps the container so you can swap the DI framework easily ) I looked into the code and I found out that the adapter only has methods to resolve objects. So I kinda wondered why there's no release method.. Does this mean this package has memory leak problems with transient lifestyle objects?
Hope somebody can give me some advice on these matters!