Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using interfaces between layers [closed]

Should communication between layers always be done using an interface or is it OK to access objects directly?

For example between the business logic and business object layers/projects.

like image 950
baileyswalk Avatar asked Feb 23 '26 00:02

baileyswalk


2 Answers

Accessing objects directly creates very tight coupling between objects - "what do you mean, you refactored Customer.FirstName to Customer.Name.GivenName? Now I have to rewrite my code, oh noes!"

Programming against an interface gives you more power to change the implementation later on, perhaps swap in a completely new one (even dynamically, at runtime). It may seem like bother, but for anything bigger than Hello World, tight coupling will come back to bite you in the butt (or whomever that maintains the code after the guy who maintained it after you).

like image 76
Piskvor left the building Avatar answered Feb 24 '26 14:02

Piskvor left the building


There are many advantages of using interface. You would do better to read up a good book on principles of object oriented technology. Or just search for why use interfaces. But the short answer is use interfaces.

like image 20
Miserable Variable Avatar answered Feb 24 '26 13:02

Miserable Variable



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!