Search code examples
c#abstract-classaccess-modifiers

C#: Possible to not implement a protected internal abstract on an abstract class?


what do I need to change in my abstract base so that the implementations don't have to implement BOTH methods when only one is needed in any given scenario? My example is:

internal abstract class BaseBar<T> where T : BaseThing
{
    protected internal abstract T DataFromXmlElements(IEnumerable<XmlNode> nodes);
    protected internal abstract T DataFromXmlElements(IEnumerable<XmlNode> nodes, string[] fieldNames);
}

class FooBarMapper : BaseBar<FooData>
{
    protected internal override SforceObjectValueData DataObjectFromXmlElements(IEnumerable<XmlNode> nodes)
    {
        throw new NotImplementedException();
    }

    protected internal override FooData DataFromXmlElements(IEnumerable<XmlNode> nodes, string[] fieldNames)
    {
        FooData o = new FooData
        {
            bar1 = bar1,
            bar2 = bar2
        };
        return o;
    }
}

Cheers.

edit: The design is weird/bad/stupid I know...I'm working with legacy code and time is not on my side right now for a refactor. I'm trying to add the second method with the string array.


Solution

  • You could make one or both virtual and provide default implementations that calls the other with default parameters (or without passing through the values, depending on which way around you put it).

    However, I suspect your design may really require a little more of a rethink because callers will likely have a reference to the base (your abstract class) and won't know they are calling a specific derived version, in which case the calling code should always be free to call which ever method it likes.