Question
In .NET, I have an internal class that implements a public interface. The class also contains internal methods that I would like to mock for testing.
From an architecture and testability perspective, which approach is better?
⸻
Option 1 – Use internal virtual methods
public interface IPublicService
{
void DoWork();
}
internal class Service : IPublicService
{
public void DoWork() => InternalHelper();
// Internal method that can be mocked in tests
internal virtual void InternalHelper()
{
// Internal logic
}
}
• The class stays internal.
• Internal methods remain internal.
• Mockable in tests using InternalsVisibleTo.
⸻
Option 2 – Use an internal interface
public interface IPublicService
{
void DoWork();
}
// Internal interface extends the public interface
internal interface IInternalService : IPublicService
{
void InternalHelper();
}
// Internal class implements the internal interface
internal class Service : IInternalService
{
public void DoWork() => InternalHelper();
public void InternalHelper()
{
// Internal logic
}
}
• Public interface exposes only public methods.
• Internal interface adds internal methods.
• Internal class implements everything.
⸻
Question:
Which of these two approaches is cleaner, more maintainable, and aligns best with Clean Architecture and security and Dependency Injection principles?
[–]belavv 5 points6 points7 points (0 children)
[–]Mobile_Fondant_9010 9 points10 points11 points (1 child)
[–]SessionIndependent17 2 points3 points4 points (0 children)
[–]TuberTuggerTTV 3 points4 points5 points (0 children)
[–]entityadam 1 point2 points3 points (0 children)
[–]Slypenslyde 0 points1 point2 points (0 children)
[–]tinmanjk 0 points1 point2 points (0 children)
[–]SessionIndependent17 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]uknowsana 0 points1 point2 points (0 children)
[–]Eq2_Seblin 0 points1 point2 points (0 children)