Вопрос

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 _tabs and _sections, which is redundant. It should look like this:

public Section()
    : this(new TabCollection(this), new SubSectionCollection(this))
{ }

public Section(TabCollection tabCollection, IList<ISection> sections)
{
    _tabs = tabCollection;
    _sections = sections;
}

This is constructor chaining, though, which is a technique used in dependency injection. Is this what you're asking about?

Другие советы

Dependency injection doesn't necessarily mean your class can't instantiate some of it's fields/properties upon construction. I typically use constructor injection for "services", not a collection of children objects.

However, I don't know all the specifics of your code, so you might want to use the Factory pattern. Something like a SectionFactory might make sense here...

public class Section
{
    internal Section(TabCollection tabColl, SectionCollection subSections)
    {
        // check for null, etc.

        // do whatever you need to do to finish construction
        tabColl.Parent = this;
        subSections.Parent = this;
    }
}

public class SectionFactory : ISectionFactory
{
    public Section Create()
    {
        var tabs = new TabCollection();
        var subs = new SectionCollection();

        return new Section(tabs, subs);
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top