Data Acquisition Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos
CurtisHx

Please use the "sealed" keyword to stop method overriding, instead of hiding the constructor.

Status: New

In order to TDD code, one has to be able to isolate dependencies.  A very handy trick to isolate external dependencies is to create an interface with the same methods and properties signatures as the external class, and then create an empty class which inherits from the external dependency and implements the interface.

 

For example, I'm trying to isolate the HardwareResourceBase class, because I'm attempting to test some code the uses the result of SystemConfiguration.FindHardware("").

 

However, I cannot isolate the HardwareResourceBase class by doing:

public interface IHardwareResourceBase
{
  String UserAlias{get;}
  String MacAddress{get;|
  /* Other methods and properties of HardwareResourceBase */
}

public class HardwareResourceImpl : HardwareResourceBase, IHardwareResourceBase
{}

because the constructor to HardwareResourceBase is hidden.  In order to isolate HardwareResourceBase, I now have to create a full wrapper class, which is a major pain.

 

Please do not fully seal classes by hiding the constructor.  If a method should not be overriden, use the "sealed" keyword.