SOLID principles in .NET revisited part 5: the Liskov Substitution Principle 2
May 7, 2015 1 Comment
Introduction
In the previous post we looked at letter ‘L’ in SOLID, i.e. the Liskov Substitution Principle. We saw how adhering to the LSP helps remove implementation-specific details from a client class that consumes those implementations. The client class will be able to consume the services of an abstraction without worrying about the actual implementation of the abstraction. The goal of LSP is that any implementation of an abstraction will work seamlessly within the consuming class.
We looked at the following cases that can indicate a violation of LSP:
- switch or if-else blocks checking for the value of an enumeration
- Code blocks that check the actual type of an abstraction to branch logic. This can be coupled with downcasting the abstraction to a concrete implementation type
- Blocks with magic strings that check for a value in some string parameter and branch logic accordingly
There’s at least one more indicator which we haven’t seen so far. I decided to devote a separate article to that case as it brings us closer to the next constituent of SOLID, i.e. the Interface Segregation Principle.