I cant seem to understand the SOLID principles
I cant seem to understand the SOLID principles, I get the idea, but idk how to write the code with them, so any exemples or someone willing to explain them better to an ict student that is trying to keep up. I keep reading articles but it doesnt help with writing my code with them, especially the OCP , ISP and the DIP
20 Replies
OCP is simple. Your classes and assemblies should be open to extension but closed to modification. That means, the user of such class/assembly should be able to extend it with their own functionality (extension methods, wrapper classes, inheritance, etc) without having to physically alter the source code you provide
ISP is also conceptually simple. No code should depend on methods it does not use. For example, you can make an interface
IAnimal
with GiveSound()
and Eat()
methods. It works for a Cat : IAnimal
, but a car should also make noise, right? You can do Car : IAnimal
for the GiveSound()
method, but that would mean the car should now also be able to eat
So an interface like that should be split
DIP is our beloved dependency inversion, or dependency injection. Basically, classes should be given their dependencies instead of creating them. Think,
instead of
which one do you think I should try to implement first? I mean whats easier to start with because I get scared just seeing how much I have to modify the code.There is a lot of inheritance in my project and im scared I ll break it :))
i tried today with the interfaces, im not sure its ok but I ll ask tomorrow at uni 🙂
Well, for starters, you don't implement SOLID
You write SOLID code
It's not really something that is applied retroactively, not often
Far as which ones are the easiest... SRP is definitely the easiest and most important, IMHO
oh sorry, im too new in this field
im trying to learn everything but its hard alone
I already have SRP
Try ISP then
But make sure you actually, you know, need to change anything there
Don't split interfaces just for the sake of it
I already have the the users part of project working with sql , managers and everything, but I got cofused with these principles a bit
It's a web app, then?
ASP.NET Core? Or something older, since you mentioned uni
Its a project that has both web app and desktop, like i have to create employees and products in the desktop, but in the web its gonna be a web shop with the products already created in the desktop, so it also has sql and asp .net
Gotcha
Recent versions of ASP come with a dependency injection frameworm built-in
So as long as you use it, that's DIP covered
tbh I cant really find anything online about asp .net, everything its for mvc, but now that you told me that, im gonna ask maybe the teacher about it since he didnt mention it
Ah, you're using the old old ASP.NET?
YEAH
The .NET Framework one?
fs
Ah, well, rip
ffs
My condolences
💀
I have absolutely zero idea about it, so I won't be of much help there
yeah the thing is that no one really knows anything about it, cause you cant find anything online
why tf do I have to use that, idk.....
that is why im very much so confused with everything revolving around this project