560 likes | 1.19k Views
Learning SOLID Principles Using C#. Dhananjay Kumar Infragistics Consultant Microsoft MVP, C# Corner MVP http :// debugmode.net. I am Dhananjay Kumar. 6 times Microsoft MVP Infragistics Consultant 7 times C- SharpCorner MVP @ debug_mode Blog : http ://debugmode.net. Agenda.
E N D
Learning SOLID Principles Using C# Dhananjay Kumar Infragistics Consultant Microsoft MVP, C# Corner MVP http://debugmode.net
I am Dhananjay Kumar • 6 times Microsoft MVP • Infragistics Consultant • 7 times C-SharpCorner MVP • @debug_mode • Blog : http://debugmode.net
Agenda SOLID Principles
SRP • Responsibilities examples • Persistence • Validation • Notification • Error Handling • Logging • Class Selection / Instantiation • Formatting • Parsing • Mapping • Sending mail • Printing etc. There are more than on functions (reason to change) in the class. Even if we change one of the functions, we will have to RETEST the whole class. Assume we have four functions(reason to change) in a class. Change in single function will cause re-testing of all four functions, which is not desirable. SRP avoids it.
SRP Demo
OCP Class should be designed in such a way that, it should be Closed for modification Open for extension
OCP Consider a scenario • There is a class with the responsibility to calculate discount • At the time of the class designing you have three rules for the discount offer. • Discounts are calculated on various rulesand that can be implemented as shown below , • if(rule 1 ) then give discount 1 • else if(rule 2 ) then give discount 2 • else if (rule 3) then give discount 3 • and so on ……….. • If a new rule is coming in the system , you will go and add new else if in the above code • By adding new else if , you will modify the existing class. • Once modified you have to again test all the rules for the better design
OCP Demo
LSP Class should be designed in such a way that- object of derived class should able to replace object of the base class without bringing any error in the system or modifying the behavior of the base class. If S is subset of T then, object of T could be replaced by object of S without impacting the program and bringing any error in the system
LSP • classical : • is- a relationship • LS principle : • is-substitute for relationship Class should be designed in such a way that- Inheritance
LSP Demo
ISP ISP splits interfaces which are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them The ISP says , clients should not be forced to depend on the methods they do not use.
ISP Demo
DIP Abstractions should not depend on details. Details should depend on abstractions. High-level modules should not depend on low-level modules. Both should depend on abstractions.
DIP Let us understand with the Demo
Summary • Single Responsibility principle • Open-Closed principle • Liskov Substitution principle • Interface Segregation principle • Dependency Inversion principle
What Infragistics can offer you? • We welcome all of you to take advantage of a FREE 30 Day Trial by downloading the product at: http://www.infragistics.com/products/ultimate/download • Please reach out to us at Sales-India@infragistics.comfor any follow up questions you may have. We welcome the opportunity to assist you.