0

I made a webapi with .NET Core and I have a "Startup" class. This class has a "ConfigureServices" method that says:

// This method gets called by the runtime. Use this method to add services to the container.

But every time i add a new service, i need to "modify" this method, but it violates the open/closed principle.

If I use use and "IF" and "ELSE" criteria to do the same thing as I am modifying this method.

So what do you think about this?

Andrews
  • 33
  • 6

1 Answers1

1

It is quite the opposite. You are extending the method inside your own Startup, not modifying the base Startup. This article from wikipedia clearly states that in order to comply to open/closed principle

software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification

I didn't quite understand what you mean with the if/else part.

markovic-m
  • 161
  • 1
  • 1
  • 7
  • About the "if else", most of the "Open/Closed Principle" examples show examples of removing the "if else" and creating abstractions such as abstract classes or interfaces. But then if my class is extended from a base class, it doesn't matter if I drop the "if else"? – Andrews Oct 30 '21 at 13:20
  • From what you said then, the base class would be like a "closed" class, no longer modifiable, while the inherited classes would be like an "open" class, modifiable at any time? – Andrews Oct 30 '21 at 13:22
  • @Andrews Yes, and no. Closed would mean that the current behavior shouldn't be modifiable (or simplified - you shouldn't/shouldn't be able to modify the existing methods/functions), but should be extendable (in our simplification - you should be allowed to add new functions/methods as well as properties). Most often, this is explained with inheritance example (even though it is not limited to it). And there you have base class which is "unmodifiable" part and you extend its behavior in subclasses, eliminating need for if/else as a result. So the if/else elimination is a (positive) side effect. – markovic-m Oct 30 '21 at 22:46