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;
  • 3
    You could group the interfaces which have something in common in an object. If they don't, you should either leave it as is or think about your design. Is your Foo class perhaps doing too much? If that's the case, spliting it into several smaller classes might solve your problem. – Andy Jan 27 '16 at 17:23

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.