SOLID is an acronym for five design principles, introduced by Robert C. Martin, for writing maintainable object-oriented code. 1. Single Responsibility Principle (SRP) A class should have only one reason to change. Example: UserService should not also send emails and write logs; move email sending to EmailService. 2. Open/Closed Principle (OCP) Software entities should be open for extension but closed for modification. Example: instead of a switch on payment type inside CheckoutService, define IPaymentMethod and add UpiPayment or CardPayment classes. New payment types do not require changing existing code. 3. Liskov Substitution Principle (LSP) Subtypes must be usable wherever the base type is expected without breaking correctness. Example: if a subclass of Bird throws on Fly(), any code that calls Fly() on birds breaks. Better: separate IFlyingBird from Bird. 4. Interface Segregation Principle (ISP) Clients should not be forced to depend on interfaces they do not use. Prefer small, focused interfaces. Example: split IMultiFunctionDevice { Print(); Scan(); Fax(); } into IPrinter, IScanner and IFax, so a simple printer class implements only IPrinter. 5. Dependency Inversion Principle (DIP) High-level modules should depend on abstractions, not on low-level details; details should depend on abstractions. Example: public class ReportService(IReportRepository repo, IReportExporter exporter) { ... } builder.Services.AddScoped<IReportRepository, SqlReportRepository>(); builder.Services.AddScoped<IReportExporter, ExcelReportExporter>(); The service does not know about SQL or Excel, so both can be replaced and unit tests can use fakes. Why SOLID matters - Code is easier to test, because dependencies can be mocked. - Changes stay local and do not ripple through the system. - New features are added by extension rather than editing fragile code. - Teams can work on separate classes with fewer conflicts. Balanced view for interviews: SOLID principles are guidelines, not rules to apply mechanically. Over-applying them creates too many tiny classes and interfaces. Use them where change is expected or complexity is real, and give one concrete example from your own project for at least one principle.
Ravindra Wadile · · 0 likes