I'm using Microsoft Unity as my IoC container. I have a number of extension classes which adds useful methods to my business objects This is the sort of code I use today:
public static class BusinessObjectExtensions
{
public static bool CanDoStuff(this BusinessObject obj)
{
var repository = BusinessHost.Resolver.Resolve<IRepository>();
var args = new EArgument { Name = obj.Name };
return repository.AMethod(obj.UserName, args);
}
}
Is there a better way to manage 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.
Why would you do that?
This raises the coupling in your application to the roof and can be very confusing for your teammates to use the extension method (they'll have to keep in mind to inject the repository each time the method is used).
Instead, create a separate class and use constructor injection to inject the IRepository
instance:
public class StuffExecuter
{
private readonly IRepository _repository;
public StuffExecuter(IRepository repository)
{
_repository = repository;
}
public bool CanExecute(BusinessObject obj)
{
_repository.Add(obj.UserName, new EArgument
{
Name = obj.Name
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With