Neat Way to Pass Interface Parameter to a Constructor

cinterfacesnetrepository

Having lots of Interface that needs to pass in a constructor looks messy, is there any neat way of doing it?

Code snippet:

public class Foo
{
    private readonly IRepository1 _repository1;
    private readonly IRepository2 _repository2;
    private readonly IRepository3 _repository3;
    private readonly IRepository4 _repository4;

    public Foo(IRepository1 repository1, IRepository2 repository2, 
               IRepository3 repository3, IRepository4  repository4)
    {
        repository1 = repository1;
        repository2 = repository2;
        repository3 = repository3;
        repository4 = repository4;
    }
}

Best Answer

Primary Constructors would have helped, but they were pulled from C# 6.

You could use certain IOC container black magic to make it look neater.

But neither remove the underlying problem: your class is awkward because it has a bunch of dependencies. If it has a bunch of dependencies, it's probably trying to do too much, since few problems require 4 completely indpendent things as inputs. You can shuffle things around, but without addressing the core problem, you're going to get code smells of some sort.