Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;
    }
}
share|improve this question
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. – David Packer Jan 27 at 17:23
up vote 2 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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