Dependency injection for extension classes?
The de facto default way of Dependency Injection by Constructor Injection is not possible for static classes. It would be possible to use Parameter Injection like below, however that is not a very clean way.
public static class BusinessObjectExtensions
{
public static bool CanDoStuff(this BusinessObject obj, IRepository repository)
{
var args = new EArgument { Name = obj.Name };
return repository.AMethod(obj.UserName, args);
}
}
You should actually try to avoid extensionmethods unless they only work on internal data (properties in the class itself), or simple datatypes provided in the method. You should not talk to other dependencies in your extension methods. If you follow this rule, you should not need to inject extension-classes with your IoC at all.