Definition - What does Shim mean?
Shim, in C#, is a template class that is derived from a base class with derived classes that inherit the data and behavior of the base class and vary only in the type. The derived class of the shim class reuses the implementation provided by the shim class.
Shim can be designed to act as a thin compatibility layer, which can resolve the compatibility issues that may arise during the development of newer versions of applications. It is also used to execute applications on software platforms for which they were not originally designed.
In general, the shim class is mainly used to provide access to functionality only through the derived classes of the shim class. This approach helps provide code extensibility. It also prevents code duplication and avoids errors due to cut-and-paste. This can mean lower costs and less maintenance.
Techopedia explains Shim
A shim class is a template class that takes type as a parameter. It can be used at any level of an inheritance chain and can provide better maintenance and extension of the hierarchy.
A shim class can be implemented using containment and aggregation methods. In case of a containment approach, the shim class passes on its calls to the re-implementation of the interface to provide additional behavior. In the aggregation method, the interface to the inner class is passed to the outer object and there is no specialization of the behavior of the inner object.
For example, Wine is a shim that enables many Windows applications to execute on operating systems like Linux, Solaris, etc.
In cases where the call to unmanaged code needs to be forwarded from managed code after some initializations, shim class can be used as a wrapper class. This shim class can be created as a more generic class that cannot be used directly but allows for the generation of derived classes that have common functionality. It helps build a framework wherein the shim class forms the link between the base class that contains common functionalities and the derived classes that have customized functions.
A COM shim that links a Microsoft Office host application and managed extension helps provide security and isolates multiple Microsoft Office extensions.