Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection for extension classes?

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?

like image 247
Leonard Avatar asked Mar 08 '16 10:03

Leonard


Video Answer


3 Answers

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);
    }
}
like image 140
mnwsmit Avatar answered Oct 24 '22 03:10

mnwsmit


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.

like image 22
andreasnico Avatar answered Oct 24 '22 03:10

andreasnico


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
        });
    }
}
like image 5
RePierre Avatar answered Oct 24 '22 01:10

RePierre