Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

should every single parameter use an interface?

Tags:

c#

interface

If i have a method like:

public void AddProduct(Product product)
{

}

Should I have all my classes implement an interface so I can do:

public void AddProduct(IProduct product)
{

}

(where product is an entity (maps to a db table)

like image 946
mrblah Avatar asked Nov 21 '25 08:11

mrblah


1 Answers

There's never a silver bullet. Interfaces and base-classes together can cover near all needs, but interfaces never fully replace base classes and vice-versa.

The potential risk of "over-interfacing" is that interfaces are difficult to change down the road, especially as they propagate into more an more code. If IProduct needs a new property then adding it to the interface has the potential to require widespread changes. Base-classes (abstract or otherwise) can remedy this by making it easy to change the base without affecting subclasses.

On the flip-side .NET doesn't allow for multiple inheritance, so base-classing is far from a silverbullet as well.


An interesting side-note: I don't have a source immediately available but one of the framework fathers (Kwalina or Abrams or a peer of theirs) notes that interfaces were introduced to .NET largely as an alternative to multiple-inheritance; and the tone was that if .NET were to be rewritten from scratch it would likely utilize multiple-inheritance.

like image 115
STW Avatar answered Nov 23 '25 22:11

STW



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!