SlideShare a Scribd company logo
1 of 43
Mastering SOLID
Strategies for Implementing
Object-Oriented Design
Principles in .NET C#
Cine sunt?
Founder Techwise SRL
Microsoft Certified Professional
Software Architect
Cine suntem?
Despre ce vom vorbi azi?
Bridge Design Pattern
Object Oriented Programming
SOLID Principles
 Single Responsibility Principle
 Open Closed Principle
 Liskov Substitution Principle
 Interface Segregation Principle
 Dependency Inversion Principle
Q and A
Bridge Design Pattern
 Structural Design Pattern.
 Decouples an abstraction from its
implementation so that the two can
vary independently.
public class RedColor { }
public class BlueColor { }
public class Shape { }
public class RedCircle : Shape
{
public RedColor Color { get; set; }
}
public class BlueCircle : Shape
{
public BlueColor Color { get; set; }
}
public class RedSquare : Shape
{
public RedColor Color { get; set; }
}
public class BlueSquare : Shape
{
public BlueColor Color { get; set; }
}
public interface IColor { }
public class RedColor : IColor { }
public class BlueColor : IColor { }
public class Shape
{
public IColor Color { get; set; }
}
public class Circle : Shape { }
public class Square : Shape { }
Shape redCircle = new Circle
{
Color = new RedColor();
}
Shape blueCircle = new Circle
{
Color = new BlueCircle();
}
Object Oriented Programming
Polymorphism
public string ReadValue(string value)
{
return value;
}
Polymorphism – 1. Override
public virtual string ReadValue(string value)
{
return value;
}
public override string ReadValue(string value)
{
return "Value: " + base.ReadValue(value);
}
Polymorphism
public string ReadValue(Reader reader)
{
return reader.Read();
}
Polymorphism – 2.Bridge
public string ReadValue(IReader reader)
{
return reader.Read();
}
Polymorphism – 3.Delegates
public string ReadValue(Func<string> readerFunc)
{
return readerFunc();
}
SOLID Principles
Were introduced in paper
”Desing Principles and Design
Patterns” By Robert C. Martin.
SOLID Principles
 Single Responsibility Principle
 Open Closed Principle
 Liskov Substitution Principle
 Interface Segregation Principle
 Dependency Inversion Principle
Single Responsibility Principle
 "Every software class should have only
one reason to change.“
 This means that every class should have
 Everything in that class should be
purpose.
public class UserCreateCommand
{
public void Create(User user)
{
InsertIntoDatabase(user);
SendNotificationEmail(user);
}
private void InsertIntoDatabase(User user) { }
private void SendNotificationEmail(User user) { }
}
public class UserCreateCommand
{
public void Create(User user)
{
InsertIntoDatabase(user);
SendNotificationEmail(user);
}
private void InsertIntoDatabase(User user) { }
private void SendNotificationEmail(User user) { }
}
public class EmailSender
{
public void SendNotificationEmail(User user) { }
}
public class UserCreateCommand
{
private readonly EmailSender _emailSender;
public UserCreateCommand()
{
_emailSender = new EmailSender();
}
public void Create(User user)
{
InsertIntoDatabase(user);
_emailSender.SendNotificationEmail(user);
}
}
Open Closed Principle
 "A software module/class is open for extension and
closed for modification.“
 "Open for extension" means we must design our
new functionality can be added only when new
 "Closed for modification" means once we developed a
gone through unit testing, then we should not alter it
specific change request.
OCP - Contracts
public class GetPersonResponseDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
OCP - Contracts
public class GetPersonResponseDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Age { get; set; }
public string Address { get; set; }
}
OCP - Classes
public class Repository<T>
{
public void Save(T entity) { }
}
OCP - Classes
public class Repository<T>
{
public int Save(T entity) { }
public void Delete(T entity) { }
}
OCP – Classes - Polimorphism
public class Repository<T>
{
public virtual void Save(T entity) { }
}
public class ExtendedRepository<T> : Repository<T>
{
public override void Save(T entity)
{
// do stuff
base.Save(entity);
}
}
Liskov Substitution Principle
 “You should be able to use any derived class instead of a
parent class and have it behave in the same manner without
manner without modification“.
 It ensures that a derived class does not affect the behavior of the
parent class.
 This principle is just an extension of the Open Closed Principle,
and we must ensure that newly derived classes extend the base
classes without changing their behavior.
Liskov Substitution Principle
public class RedColor
{
public void Splash() { }
public virtual string GetColor()
{
return "Red";
}
}
public class BlueColor : RedColor
{
public override string GetColor()
{
return "Blue";
}
}
Liskov Substitution Principle
RedColor redColor = new BlueColor();
redColor.GetColor(); // Blue
Liskov Substitution Principle
public abstract class ColorBase
{
public void Splash() { }
public abstract string GetColor();
}
public class RedColor : ColorBase
{
public override string GetColor()
{
return "Red";
}
}
public class BlueColor : ColorBase
{
public override string GetColor()
{
return "Blue";
}
}
Interface Segregation Principle
 “Clients should not be forced to
implement interfaces they don't use”.
 Instead of a header interface, many small
preferred based on groups of methods,
one submodule.
Interface Segregation Principle
public interface IShape
{
string GetColor();
void SetColor();
int GetNumberOfSides();
}
Interface Segregation Principle
public interface IGetColor
{
string GetColor();
}
public interface ISetColor
{
void SetColor();
}
public interface IGetNumberOfSides
{
int GetNumberOfSides();
}
Interface Segregation Principle
public interface IColor : IGetColor, ISetColor
{
}
public interface IShape: IColor, IGetNumberOfSides
{
}
Interface Segregation Principle
public class Shape : IShape { }
Shape shape = new Shape();
IColor color = shape as IColor;
IGetColor getColor = shape as IGetColor;
ISetColor setColor = shape as ISetColor;
IGetNumberOfSides getNumberOfSides = shape as IGetNumberOfSides;
Dependency Inversion Principle
 "Depend upon abstractions, not concretions.“
 Favor composition over inheritance.
public class EmailSender
{
public void SendNotificationEmail(User user) { }
}
public class UserCreateCommand
{
private readonly EmailSender _emailSender;
public UserCreateCommand()
{
_emailSender = new EmailSender();
}
public void Create(User user)
{
InsertIntoDatabase(user);
_emailSender.SendNotificationEmail(user);
}
}
public interface IEmailSender
{
void SendNotificationEmail(User user) { }
}
public class UserCreateCommand
{
private readonly IEmailSender _emailSender;
public UserCreateCommand(IEmailSender emailSender)
{
_emailSender = emailSender;
}
public void Create(User user)
{
InsertIntoDatabase(user);
_emailSender.SendNotificationEmail(user);
}
}
Recap
 Single Responsibility Principle
 Open Closed Principle
 Liskov Substitution Principle
 Interface Segregation Principle
 Dependency Inversion Principle
Va multumesc,
Andrei Iacob
 https://www.linkedin.com/in/iacob-dan-andrei/
 Tel: 0729929314

More Related Content

Similar to Andrei Iacob - SOLID: Strategies for Implementing Object–Oriented Design Principles in .NET C#

Presentation on design pattern software project lll
 Presentation on design pattern  software project lll  Presentation on design pattern  software project lll
Presentation on design pattern software project lll Uchiha Shahin
 
Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014Antoine Sabot-Durand
 
Introduction to Object oriented Design
Introduction to Object oriented DesignIntroduction to Object oriented Design
Introduction to Object oriented DesignAmin Shahnazari
 
Solid Principles
Solid PrinciplesSolid Principles
Solid PrinciplesHitheshh
 
My way to clean android v2 English DroidCon Spain
My way to clean android v2 English DroidCon SpainMy way to clean android v2 English DroidCon Spain
My way to clean android v2 English DroidCon SpainChristian Panadero
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodeSWIFTotter Solutions
 
Clean Code Part I - Design Patterns at SoCal Code Camp
Clean Code Part I - Design Patterns at SoCal Code CampClean Code Part I - Design Patterns at SoCal Code Camp
Clean Code Part I - Design Patterns at SoCal Code CampTheo Jungeblut
 
Parsley & Flex
Parsley & FlexParsley & Flex
Parsley & Flexprideconan
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.pptEmanAsem4
 
Mieux programmer grâce aux design patterns
Mieux programmer grâce aux design patternsMieux programmer grâce aux design patterns
Mieux programmer grâce aux design patternsGeeks Anonymes
 
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...Theo Jungeblut
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asmaAbdullahJana
 
1/3 : introduction to CDI - Antoine Sabot-Durand
1/3 : introduction to CDI - Antoine Sabot-Durand1/3 : introduction to CDI - Antoine Sabot-Durand
1/3 : introduction to CDI - Antoine Sabot-DurandSOAT
 

Similar to Andrei Iacob - SOLID: Strategies for Implementing Object–Oriented Design Principles in .NET C# (20)

Presentation on design pattern software project lll
 Presentation on design pattern  software project lll  Presentation on design pattern  software project lll
Presentation on design pattern software project lll
 
Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014Introduction to cdi given at java one 2014
Introduction to cdi given at java one 2014
 
Introduction to Object oriented Design
Introduction to Object oriented DesignIntroduction to Object oriented Design
Introduction to Object oriented Design
 
Hexagonal architecture in PHP
Hexagonal architecture in PHPHexagonal architecture in PHP
Hexagonal architecture in PHP
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
My way to clean android v2 English DroidCon Spain
My way to clean android v2 English DroidCon SpainMy way to clean android v2 English DroidCon Spain
My way to clean android v2 English DroidCon Spain
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better Code
 
Builder design pattern
Builder design patternBuilder design pattern
Builder design pattern
 
CDI in JEE6
CDI in JEE6CDI in JEE6
CDI in JEE6
 
My way to clean android V2
My way to clean android V2My way to clean android V2
My way to clean android V2
 
Clean Code Part I - Design Patterns at SoCal Code Camp
Clean Code Part I - Design Patterns at SoCal Code CampClean Code Part I - Design Patterns at SoCal Code Camp
Clean Code Part I - Design Patterns at SoCal Code Camp
 
Parsley & Flex
Parsley & FlexParsley & Flex
Parsley & Flex
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Mieux programmer grâce aux design patterns
Mieux programmer grâce aux design patternsMieux programmer grâce aux design patterns
Mieux programmer grâce aux design patterns
 
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
Clean Code I - Design Patterns and Best Practices at SoCal Code Camp San Dieg...
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
 
1/3 : introduction to CDI - Antoine Sabot-Durand
1/3 : introduction to CDI - Antoine Sabot-Durand1/3 : introduction to CDI - Antoine Sabot-Durand
1/3 : introduction to CDI - Antoine Sabot-Durand
 
Polymorphism (2)
Polymorphism (2)Polymorphism (2)
Polymorphism (2)
 

Recently uploaded

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 

Recently uploaded (20)

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 

Andrei Iacob - SOLID: Strategies for Implementing Object–Oriented Design Principles in .NET C#

  • 1. Mastering SOLID Strategies for Implementing Object-Oriented Design Principles in .NET C#
  • 2. Cine sunt? Founder Techwise SRL Microsoft Certified Professional Software Architect
  • 4. Despre ce vom vorbi azi? Bridge Design Pattern Object Oriented Programming SOLID Principles  Single Responsibility Principle  Open Closed Principle  Liskov Substitution Principle  Interface Segregation Principle  Dependency Inversion Principle Q and A
  • 5. Bridge Design Pattern  Structural Design Pattern.  Decouples an abstraction from its implementation so that the two can vary independently.
  • 6.
  • 7. public class RedColor { } public class BlueColor { } public class Shape { } public class RedCircle : Shape { public RedColor Color { get; set; } } public class BlueCircle : Shape { public BlueColor Color { get; set; } } public class RedSquare : Shape { public RedColor Color { get; set; } } public class BlueSquare : Shape { public BlueColor Color { get; set; } }
  • 8.
  • 9. public interface IColor { } public class RedColor : IColor { } public class BlueColor : IColor { } public class Shape { public IColor Color { get; set; } } public class Circle : Shape { } public class Square : Shape { }
  • 10. Shape redCircle = new Circle { Color = new RedColor(); } Shape blueCircle = new Circle { Color = new BlueCircle(); }
  • 11.
  • 14. Polymorphism – 1. Override public virtual string ReadValue(string value) { return value; } public override string ReadValue(string value) { return "Value: " + base.ReadValue(value); }
  • 15. Polymorphism public string ReadValue(Reader reader) { return reader.Read(); }
  • 16. Polymorphism – 2.Bridge public string ReadValue(IReader reader) { return reader.Read(); }
  • 17. Polymorphism – 3.Delegates public string ReadValue(Func<string> readerFunc) { return readerFunc(); }
  • 18. SOLID Principles Were introduced in paper ”Desing Principles and Design Patterns” By Robert C. Martin.
  • 19. SOLID Principles  Single Responsibility Principle  Open Closed Principle  Liskov Substitution Principle  Interface Segregation Principle  Dependency Inversion Principle
  • 20. Single Responsibility Principle  "Every software class should have only one reason to change.“  This means that every class should have  Everything in that class should be purpose.
  • 21. public class UserCreateCommand { public void Create(User user) { InsertIntoDatabase(user); SendNotificationEmail(user); } private void InsertIntoDatabase(User user) { } private void SendNotificationEmail(User user) { } }
  • 22. public class UserCreateCommand { public void Create(User user) { InsertIntoDatabase(user); SendNotificationEmail(user); } private void InsertIntoDatabase(User user) { } private void SendNotificationEmail(User user) { } }
  • 23. public class EmailSender { public void SendNotificationEmail(User user) { } } public class UserCreateCommand { private readonly EmailSender _emailSender; public UserCreateCommand() { _emailSender = new EmailSender(); } public void Create(User user) { InsertIntoDatabase(user); _emailSender.SendNotificationEmail(user); } }
  • 24. Open Closed Principle  "A software module/class is open for extension and closed for modification.“  "Open for extension" means we must design our new functionality can be added only when new  "Closed for modification" means once we developed a gone through unit testing, then we should not alter it specific change request.
  • 25. OCP - Contracts public class GetPersonResponseDto { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } }
  • 26. OCP - Contracts public class GetPersonResponseDto { public string FirstName { get; set; } public string LastName { get; set; } public string Age { get; set; } public string Address { get; set; } }
  • 27. OCP - Classes public class Repository<T> { public void Save(T entity) { } }
  • 28. OCP - Classes public class Repository<T> { public int Save(T entity) { } public void Delete(T entity) { } }
  • 29. OCP – Classes - Polimorphism public class Repository<T> { public virtual void Save(T entity) { } } public class ExtendedRepository<T> : Repository<T> { public override void Save(T entity) { // do stuff base.Save(entity); } }
  • 30. Liskov Substitution Principle  “You should be able to use any derived class instead of a parent class and have it behave in the same manner without manner without modification“.  It ensures that a derived class does not affect the behavior of the parent class.  This principle is just an extension of the Open Closed Principle, and we must ensure that newly derived classes extend the base classes without changing their behavior.
  • 31. Liskov Substitution Principle public class RedColor { public void Splash() { } public virtual string GetColor() { return "Red"; } } public class BlueColor : RedColor { public override string GetColor() { return "Blue"; } }
  • 32. Liskov Substitution Principle RedColor redColor = new BlueColor(); redColor.GetColor(); // Blue
  • 33. Liskov Substitution Principle public abstract class ColorBase { public void Splash() { } public abstract string GetColor(); } public class RedColor : ColorBase { public override string GetColor() { return "Red"; } } public class BlueColor : ColorBase { public override string GetColor() { return "Blue"; } }
  • 34. Interface Segregation Principle  “Clients should not be forced to implement interfaces they don't use”.  Instead of a header interface, many small preferred based on groups of methods, one submodule.
  • 35. Interface Segregation Principle public interface IShape { string GetColor(); void SetColor(); int GetNumberOfSides(); }
  • 36. Interface Segregation Principle public interface IGetColor { string GetColor(); } public interface ISetColor { void SetColor(); } public interface IGetNumberOfSides { int GetNumberOfSides(); }
  • 37. Interface Segregation Principle public interface IColor : IGetColor, ISetColor { } public interface IShape: IColor, IGetNumberOfSides { }
  • 38. Interface Segregation Principle public class Shape : IShape { } Shape shape = new Shape(); IColor color = shape as IColor; IGetColor getColor = shape as IGetColor; ISetColor setColor = shape as ISetColor; IGetNumberOfSides getNumberOfSides = shape as IGetNumberOfSides;
  • 39. Dependency Inversion Principle  "Depend upon abstractions, not concretions.“  Favor composition over inheritance.
  • 40. public class EmailSender { public void SendNotificationEmail(User user) { } } public class UserCreateCommand { private readonly EmailSender _emailSender; public UserCreateCommand() { _emailSender = new EmailSender(); } public void Create(User user) { InsertIntoDatabase(user); _emailSender.SendNotificationEmail(user); } }
  • 41. public interface IEmailSender { void SendNotificationEmail(User user) { } } public class UserCreateCommand { private readonly IEmailSender _emailSender; public UserCreateCommand(IEmailSender emailSender) { _emailSender = emailSender; } public void Create(User user) { InsertIntoDatabase(user); _emailSender.SendNotificationEmail(user); } }
  • 42. Recap  Single Responsibility Principle  Open Closed Principle  Liskov Substitution Principle  Interface Segregation Principle  Dependency Inversion Principle
  • 43. Va multumesc, Andrei Iacob  https://www.linkedin.com/in/iacob-dan-andrei/  Tel: 0729929314