Search code examples
c#architectureinterfaceinterface-design

Best practices when using an interface


Many times when designing interfaces I keep running into the same situation. The situation is where certain implementations using an interface require particular parameters in the interface while others do not.

  • What is the best practice when designing an interface?
  • Is it OK to have certain implementations that implement the interface but not use all the parameters?

Or in these situations should I just be taking in a list (some structure) of parameters and deal with that list accordingly in each implementation?


Solution

  • No it's not OK. It breaks Liskovs Substituion Principle.

    Sounds to me that your interfaces are trying to do too much. Either use interface inheritance or split the interface into multiple ones. Do note that it's better having many small interfaces than one large. Classes can still implement all of those.

    Interfaces, like classes, should follow SRP (Single Responsibility Principle). imho it's much much more important that interfaces do so since they force a design upon the rest of your application.

    I also tend to try to avoid adding properties as much as possible from interfaces.