I have this ctor:
public Section()
{
_tabs = new TabCollection(this);
_sections = new SubSectionCollection(this);
}
I would like to get something like this:
public Section()
: this(new TabCollection(this), new SubSectionCollection(this))
{
}
public Section(TabCollection tabCollection, IList<ISection> sections)
{
_tabs = tabCollection;
_sections = sections;
}
Of course this doesn’t work. Anyone has any suggestion how I could refactor this code?
I need to do this in order to be able to Mock an object of type Section in Unit Testing. We are using FakeItEasy testing framework.
One issue is that your first constructor, the one with no parameters, delegates to the second constructor. In other words, the second one will be invoked by the first one with the parameters in the
this()statement. Yet, the first one also contains setters for_tabsand_sections, which is redundant. It should look like this:This is constructor chaining, though, which is a technique used in dependency injection. Is this what you’re asking about?