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)
{
this.InitialiseCollection();
this.Bars.Add(bar)
}