SOLID Principles in C# .Net 6.0 Using Batman As Example
Criminals are a superstitious cowardly lot. That is why, I am going to use SOLID principles in C # and .NET Core to solve complex coding problems and write elegant code.
For some time now, I have been trying to put together a sample code for SOLID principles. This week, I finally got around to putting the finishing touches.
As always, the full code is available here.
Here is a quick run through the key concepts. My code is extremely commented, so, this would be a very long post indeed.
The single-responsibility principle
Here, the story I am spinning is that, Batman should be solely focused on battling with the bad guy. Other stuff, like, calling for help, should be handled by some Bat Gadget, which is how it should be.
The open–closed principle
Here, the story I am writing is that Batman is better of building new vehicles to fight crime, by extending an existing vehicle design. That way, the original design remains unaffected.
The Liskov substitution principle
Here, I imagine that Batman works as part of a team, the Bat Family. Every now and then, Robin and perhaps a Batman clone, will have to take over from Batman.
In such scenarios, obviously, when the original Batman returns, he should be able to replace Robin as well as Batman clone, without causing any unexpected errors.
The interface segregation principle
Here, the story is, Batman actions must be built on implementations that are in turn built on top of individual interfaces. That way, Batman can mix and match his behavior depending on what he is doing on that specific day.
The dependency inversion principle
By far, the most complex of the topics in the entire SOLID principles. I request you to spend additional time practicing this topic.
Remember that understanding dependency inversion is the key to all those dozens of incredibly boring design patterns that your employer or university will force you to learn before getting a job as a .Net developer. You can read about my views about design patterns here.
Alright, here is the code.
Now, then, here, the story is that Batman is out fighting crime. Every night, he needs someone to help out. Batman is a longer, but he does depend on people. Most of the time, he depends on his father figure, his butler, Alfred.
Now, what if he Alfred is not always there? Batman cannot ‘hard code’ his dependency on Alfred alone. So, I use dependency inversion so that batman depends on ‘someone’ to help, but the actual help is ‘injected’ at the time of need, depending on who is available.
Final Note
Being an object oriented programmer is not easy. That goes double for C Sharp in .Net. Once you have learnt the basics in C Sharp, you want to start learning SOLID principles.
Of course, you can always write good code, simply by applying common sense. However, using SOLID, will make your code better. It also helps you get ready to start learning those boring as rocks, design patterns.
I work as a full time freelance coding tutor. Hire me at UpWork or Fiverr or Stack Overflow. My personal website is here. Find more of my art at Behance and Unsplash. Also, I have a Podcast about everyday life. And, a 2nd Podcast, where I talk about freelancing.