Introductory piece of code:
class BaseClass
{
protected Foo MyFoo { get; }
}
class ChildClass : BaseClass
{
void SomeMethod()
{
MyFoo.DoStuff();
//Here, I have no idea that MyFoo is not defined in this class,
//but rather in the base class.
}
}
The problem/debate is that you have to either know or check manually where MyFoo is defined. And you're better off using two private fields, one in the base class, on in the child class.
In our case, most of the time, these properties come from dependency injection and are available in the constructor of both classes. So it's really easy to just map them to private properties from that constructor, instead of inheriting it from the base class, which is also less clear.
Is there good practice that I'm missing? What is your experience there?
Underlying question: Are protected properties just a bad habit, and should I always go for private fields instead?
Will this go against me when unit testing?
Is this considered clean/unclean code?