SOLID: The Good, The Bad & The Real World
Hey friends! Welcome to our long awaited tutorial on the principles of SOLID: single responsibility principle, open closed principle, Liskov substitution principle, interface segregation principle and, my personal favorite: the donut in face principle. Probably... actually known as the dependency inversion principle.
I want to thank my coauthor Diego for helping me finally put this tutorial together. And I'm super sorry if you've been waiting for this!
SOLID Principles: I don't Love Them
So... why did it take us so long to get this tutorial done? The short answer is: I.... kind of don't like the SOLID principles. Okay, let me rephrase that. The SOLID principles are tough to understand. And, in my most humble opinion, they're not always good advice! It depends on the situation. For example, you should write code for your application differently than you would write code that's meant to be open sourced and shared.
If you want to know a bit more about why SOLID might not always be correct, you can read a recent blog post written by Dan North called CUPID – THE BACK STORY. Dan North is known for being the person who first made behavior-driven development famous. You may have heard of him if you're a Behat user.
Anyways, this tutorial is not going to be yet another tutorial where we read the definition of each SOLID principle in a monotone voice... and slowly get lost, bored and finally fall asleep. Nope. We're going to dive into each principle, learn what they really mean - using normal human words - code some real examples and discuss why and when following these principles makes sense and does not make sense. But even when the SOLID principles should not be followed, they have a lot to teach us. So strap in for a wild ride.
Project Setup
Since we're going to be doing some real coding, let's get the project set up and rocking. Do me a solid by downloading the course code from this page and unzipping it. After you do, you'll find a start/ directory with the same code you see here. This fancy README.md file has all the details about how to get the project up and running. The last step will be to find a terminal, move into the project and start a local web server. I'll use the Symfony binary for this:
symfony serve -d
Once this finishes, copy that URL, spin back over to your browser, paste and... say hello to "Sasquatch Sightings"! Our latest effort to find the infamous Bigfoot. What this code actually does is... not too important. It talks to a database, lists some big foot sightings and has some calculations. It will be our playground for diving into the SOLID principles.
So next, let's start with the first: the single responsibility principle!
12 Comments
SALAD :)
I swear I heard SALAD too xD xD but anyways, those guys always makes the topic fun and interesting xD
SOLID and SALAD have one thing in common, both things are good for you :)
I am getting a syntax error when trying to load the database.
I have done the following:
The problem I get is when it tries to insert the data in to the 'user' table. I can't seem to find the file that has the user data or sql commands to insert the data.
Hey @JDCrain
Could you tell me what error did you get? To insert the data you need to load the fixtures (after providing the database). Run
bin/console doctrine:fixtures:load- if you're on Docker runsymfony console doctrine:fixtures:loadCheers!
I am not using Docker but I have tried both 'bin/console' and 'symfony console' and get the same error about executing the 'INSERT INTO' commands.
Here is a screenshot of the output after running the command.
Screen Capture Image
Ohh, I think it is a Postgres thing. Try surrounding the name of the table
userin backticks ( ` ). You'll need to add this to the User entity classOr, if that does not work, try renaming the table to something else. The problem is that
useris a reserved wordCheers!
That worked. I had to change the comment a little since this course is using the 5.2.6 version of Symfony.
I do everything it says on the README.md but when i try to run
symfony console doctrine:database:createi get this error:
In AbstractMySQLDriver.php line 112: An exception occurred in driver: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)<br />SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)<br />The Mysql service is running. and when I connect to mysql in my terminal using
mysql -u root -pi don't get any errorsHey @The-cocktail-E
Did you update the database connection environment variable?
DATABASE_URLyou can do so by creating a.env.localfileCheers!
ARM architecture is not working fine with mysql 5.7 but this is the solution.
Run this command on terminal
docker pull --platform linux/x86_64 mysql<br />Update the docker-compose.yaml file like that :
Now run
docker-compose up -dand
Hey @GolemAI
Thank you for sharing it! Cheers!
"Houston: no signs of life"
Start the conversation!