C
C#•8mo ago
Dalia

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
Angius
Angius•8mo ago
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,
class Foo
{
private readonly Bar _bar;
public Foo(Bar bar)
{
_bar = bar;;
}
}
var b = new Bar();
var f = new Foo(b);
class Foo
{
private readonly Bar _bar;
public Foo(Bar bar)
{
_bar = bar;;
}
}
var b = new Bar();
var f = new Foo(b);
instead of
class Foo
{
private readonly Bar _bar;
public Foo()
{
_bar = new Bar();
}
}
var f = new Foo();
class Foo
{
private readonly Bar _bar;
public Foo()
{
_bar = new Bar();
}
}
var f = new Foo();
Dalia
DaliaOP•8mo ago
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 🙂
Angius
Angius•8mo ago
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
Dalia
DaliaOP•8mo ago
oh sorry, im too new in this field im trying to learn everything but its hard alone I already have SRP
Angius
Angius•8mo ago
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
Dalia
DaliaOP•8mo ago
I already have the the users part of project working with sql , managers and everything, but I got cofused with these principles a bit
Angius
Angius•8mo ago
It's a web app, then? ASP.NET Core? Or something older, since you mentioned uni
Dalia
DaliaOP•8mo ago
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
Angius
Angius•8mo ago
Gotcha Recent versions of ASP come with a dependency injection frameworm built-in So as long as you use it, that's DIP covered
Dalia
DaliaOP•8mo ago
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
Angius
Angius•8mo ago
Ah, you're using the old old ASP.NET?
Dalia
DaliaOP•8mo ago
YEAH
Angius
Angius•8mo ago
The .NET Framework one?
Dalia
DaliaOP•8mo ago
fs
Angius
Angius•8mo ago
Ah, well, rip
Dalia
DaliaOP•8mo ago
ffs
Angius
Angius•8mo ago
My condolences
Dalia
DaliaOP•8mo ago
💀
Angius
Angius•8mo ago
I have absolutely zero idea about it, so I won't be of much help there
Dalia
DaliaOP•8mo ago
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
Want results from more Discord servers?
Add your server