Given the class:

public class Foo : IFoo
  private IBarRepository repository

  public Foo(IBarRepository repository)
     this.repository = repository

  public IList<IBar> Bars { get; private set; }

My long standing instinct is to initialise the list of IBar in the constructor: this.Bars = new List<IBar>(); but using Constructor Injection, the Single Responsibility of the constructor is for it to set the dependancies for the class.

What is the best way to handle this?

Should I have a collection initialiser that I call in any method before using the collection?

private void InitialiseCollection()
  if (this.Bars == null)
     this.Bars = new List<IBar>();

public void Add(IBar bar)

Well, the simple answer to your question is

public readonly IList<IBar> Bars = new List<IBar>();

But I think you should question your premise.

but using Constructor Injection, the Single Responsibility of the constructor is for it to set the dependancies for the class.

Where have you read that? It's entirely incorrect.

It doesn't matter if you're using Constructor Injection or not. Your constructor's responsibility is to construct an object, fully initialized, with all the dependencies required to operate on that object. Those dependencies can be other services or state or values that distinguish this object from another object of its type.

  • It's from "Dependancy Injection in .Net" by Mark Seemann, Section 4.1.1 (page 99 of my edition) – NikolaiDante Jan 6 '14 at 16:05
  • @NikolaiDante: Interesting. Mark used to be around here a lot but hasn't been much recently. With some luck, he'll notice this and be able to explain. I'd be surprised if he meant it as literally as you have taken it here. – pdr Jan 6 '14 at 16:29
  • That would be good. I've also just found a blog post of his on the subject - blog.ploeh.dk/2011/03/03/InjectionConstructorsshouldbesimple which has "It's important to understand that when using Constructor Injection the constructor should contain no additional logic.". – NikolaiDante Jan 6 '14 at 16:32
  • 1
    @NikolaiDante: That sentence is correct but, again, literally questionable. Initializing a list is not logic, at least not in the context of that sentence. Logic would be if it were calculating a single item from a list, or conditionally adding items to the list. But, arguably, all code is logic so I can see why you'd misread the intent. – pdr Jan 6 '14 at 16:49
  • @pdr +1 instantiating the list in this case is part of the "construction process" and rightfully belongs in the constructor regardless of whatever else you are injecting into it. – Marjan Venema Jan 6 '14 at 19:28

The Single Responsibility principle applies to the class as a whole, not just to the constructor. Also, resist the urge to initialize the Bars variable using that last method - if you are using injection, it should be responsible for giving you a complete and working instance of Foo or none at all if it can't resolve the object to pass to the constructor.

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.