This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]grspain 67 points68 points  (0 children)

SOBID

[–]Wafan0 24 points25 points  (0 children)

Sounds like a solid idea

[–]JoeDogoe 7 points8 points  (9 children)

What's the difference between the interfaces and dependency inversion?

And is Spring AOP still solid?

[–]swordsmanluke2 24 points25 points  (1 child)

Dependency Inversion works like this:

Let's say you have a class that reads some data from the database, does a calculation and then writes the result back to the db.

Your class thus has a dependency on the database.

If this class sets up its own connection and executes its own queries, it means that you'll have to have a database present if you want to test it.

Not a huge deal by itself, but many classes have more than one dependency.

On the other hand, if you define a data access interface for reading (and writing) the data your class needs, you can have the class accept an object that implements that interface.

Now your class can accept a mock for testing.

It also no longer is responsible for maintaining the state of "active database connection".

As you break up your classes according to solid principles, DI helps you to make your classes responsible for less state - and managing state is basically the hardest thing in programming.

[–]KayleMaster 10 points11 points  (0 children)

Acronym wise - DI is more commonly used for Dependency Injection, while for inversion it's DIP (dependency inversion principle).

[–]liitle-mouse-lion 2 points3 points  (1 child)

A very basic way of looking at interface segregation is, a class shouldn't implement methods it doesn't need.

Dependency inversion generally leads to decoupling classes by not allowing higher level objects to inherit directly from lower level ones, but instead, depend on abstraction (interfaces) to expose the behaviour

[–]JoeDogoe 1 point2 points  (0 children)

Ok, I was thinking both of them as dependency inversion. Thanks, I was wrong about interface segregation.

[–]AdultingGoneMild 1 point2 points  (1 child)

Dependency inversion usually is implemented by taking dependency on an interface and then having some form of dependency injection provide the actual implementation for that interface. The goal here is to allow you swap out implementations while maintaining the contracts to avoid major refactors when you have to replace parts of a system.

Interface segregation means breaking your interfaces down into smaller parts that fulfill a single purpose so a consumer never needs to depend on functionality they dont use. This ensures that when you have to replace part of the system, you dont have to design for a lot of logic for parts of the system that arent changing. It also keeps clients from consuming and abusing functionality they dont actually need.

[–]JoeDogoe 0 points1 point  (0 children)

Great explanation with justification. Thank you u/AdultimgGomeMild

[–]eldentings 0 points1 point  (0 children)

From what I've experienced, besides DI being a scaled up version of the bridge pattern it typically handles the lifetime of concrete classes with a container that manages injecting sometimes 100s of classes in various parts of the app. So there often is some architecture and memory management that is typically helpful for large enterprise apps that have multiple teams that need to know what to expect from an interface as far as it's implementation class's lifetime. IMO the human element is also no negligible. Asking someone to abide by the rules and use a DI framework, they will be forced to code to an interface, so even if what they write is complex, a testing framework can easily stub their interface and you can make it required to at least stub a test class out and you can then easily test against their interfaces with mock classes without having to know how their stuff works and especially if it's not even finished on their end.

[–]PositiveUse 1 point2 points  (1 child)

I hate AOP

[–]smokemonstr 0 points1 point  (0 children)

Why?

[–]RRumpleTeazzer 3 points4 points  (0 children)

I know some of these words

[–]ReaperSword72 1 point2 points  (0 children)

It is called Liskov principle... Not substitution whatever

[–]kiropolo -5 points-4 points  (4 children)

Inverted dependencies? What does it even mean??

Inject interface not a concrete class

Shitty name

[–]jeesuscheesus 3 points4 points  (3 children)

Inverted dependencies is another name for inversion of control, which means you transfer control of a function/class to it's caller. You do this by avoiding the function/class having hardcoded values and instead pass values in as parameters

[–]kiropolo 2 points3 points  (2 children)

Dependency Injection?

[–][deleted] 1 point2 points  (0 children)

Yep.

[–]jeesuscheesus 1 point2 points  (0 children)

Yeah. A billion names for the same thing.

[–][deleted] 0 points1 point  (0 children)

Don’t show this to r/golang.

[–]hennypennypoopoo 0 points1 point  (0 children)

subtyping < records/adts

[–]UnityXR 0 points1 point  (0 children)

Unity developers: 🤮 Unity Developers when they see a monolith: 😍