Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using IoC in extension methods

Tags:

I am working on an ASP MVC 3 app and I'm writing a custom html helper. It's nothing special or hugely complex, but it will need an instance of an interface from structure map. I know I can simply call into structuremaps' object factory from inside the method, but since the rest of the app uses IoC rather than service location I'd like to keep it that way.

Is there a way to inject interfaces into extension methods from inside and asp net mvc app?

UPDATE

An example of what I'm doing might help:

public static class ShowUrl {     public static string ForShow(this UrlHelper url, int showId)     {         var service = ObjectFactory.GetInstance<IPerformanceService>();          var showName = service.GetPerformanceTitle(showId);          return url.Action(MVC.Performance.Details(showId, showName.ToFriendlyUrl()));     } } 

Which is used like this:

<a href='<%= Url.ForShow(1)%>'> 

Essentially I am trying to build a URL with a slug from an entity id. Maybe I'm just going about this in a really daft way.

like image 862
ilivewithian Avatar asked Jul 06 '11 14:07

ilivewithian


People also ask

What are extension methods explain with an example?

In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type.

How extension method is achieved?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

What is extension method in OOP?

In object-oriented computer programming, an extension method is a method added to an object after the original object was compiled. The modified object is often a class, a prototype or a type. Extension methods are features of some object-oriented programming languages.

Where do you put extension methods?

An Extension Method should be in the same namespace as it is used or you need to import the namespace of the class by a using statement. You can give any name of for the class that has an Extension Method but the class should be static.


1 Answers

I would not recommend doing this. Extension methods are generally best used for simple, well-known operations directly on a type. If your extension method is dependent on having an instance of another type, it is likely that it shouldn't be an extension method to begin with.

Consider making an actual service class that performs this functionality, and injecting it where it's needed. If you really need this in an extension method, consider wrapping the functionality your extension method requires in another static class/method, and avoid using any kind of injection or location.

Sharing some code might shed more light on your specific situation.

like image 196
Phil Sandler Avatar answered Nov 11 '22 09:11

Phil Sandler