Learning Single Responsibility Principle with C#

Learning Single Responsibility Principle with C#

The Single Responsibility Principle (SRP) is one of the SOLID principles of object-oriented design. It states that a class should have only one reason to change, meaning that a class should have only one responsibility or task to fulfill. This principle helps in keeping classes focused, maintainable, and easily understandable.

To learn the Single Responsibility Principle with C#, let's consider an example of a Customer class that manages both customer data and email notifications. This violates the SRP because it has two responsibilities:

public class Customer { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } public void SaveCustomerData() { // Code to save customer data to the database } public void SendEmailNotification() { // Code to send an email notification to the customer } } 

To adhere to the Single Responsibility Principle, we should separate the responsibilities of managing customer data and sending email notifications into two separate classes:

// Customer data management class public class CustomerRepository { public void SaveCustomerData(Customer customer) { // Code to save customer data to the database } } // Email notification class public class EmailService { public void SendEmailNotification(string email, string message) { // Code to send an email notification to the given email address } } 

With this refactoring, each class now has a single responsibility:

  • CustomerRepository: Manages customer data and is responsible for saving customer data to the database.
  • EmailService: Manages email notifications and is responsible for sending email notifications.

Now, the Customer class only contains properties related to customer data, and its responsibility is limited to holding customer information:

public class Customer { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } } 

This separation of concerns makes the code more maintainable, easier to understand, and allows for better reusability. It also follows the Single Responsibility Principle, where each class has a single and well-defined responsibility.

Examples

  1. "Single Responsibility Principle in C# definition"

    • Description: Understand the Single Responsibility Principle in C# and its definition.
    // Code demonstrating Single Responsibility Principle public class Customer { public void AddCustomer() { // Add customer logic } public void GenerateInvoice() { // Generate invoice logic } } 
  2. "C# Single Responsibility Principle example with a class"

    • Description: Learn how to apply the Single Responsibility Principle to a class in C#.
    // Code illustrating Single Responsibility Principle with separate responsibilities public class Customer { public void AddCustomer() { // Add customer logic } } public class InvoiceGenerator { public void GenerateInvoice() { // Generate invoice logic } } 
  3. "Real-world Single Responsibility Principle scenario in C#"

    • Description: Explore a real-world scenario demonstrating the Single Responsibility Principle in C#.
    // Code showing Single Responsibility Principle with logging responsibility separated public class Customer { private ILogger logger; public Customer(ILogger logger) { this.logger = logger; } public void AddCustomer() { // Add customer logic logger.Log("Customer added successfully."); } } 
  4. "C# Single Responsibility Principle in interfaces"

    • Description: Implement the Single Responsibility Principle using interfaces in C#.
    // Code demonstrating Single Responsibility Principle with separate interfaces public interface ICustomerRepository { void AddCustomer(); } public interface IInvoiceGenerator { void GenerateInvoice(); } 
  5. "Applying Single Responsibility Principle to methods in C#"

    • Description: Learn how to apply the Single Responsibility Principle to methods within a class in C#.
    // Code illustrating Single Responsibility Principle with separate methods public class Customer { public void AddCustomer() { // Add customer logic LogCustomerAdded(); } private void LogCustomerAdded() { // Logging logic } } 
  6. "C# Single Responsibility Principle violation example"

    • Description: Identify and understand a violation of the Single Responsibility Principle in C#.
    // Code violating Single Responsibility Principle public class Customer { public void AddCustomer() { // Add customer and log in the same method } } 
  7. "Refactoring code to adhere to Single Responsibility Principle in C#"

    • Description: Explore the process of refactoring code to adhere to the Single Responsibility Principle in C#.
    // Code refactored to follow Single Responsibility Principle public class Customer { private ILogger logger; public Customer(ILogger logger) { this.logger = logger; } public void AddCustomer() { // Add customer logic logger.Log("Customer added successfully."); } } 
  8. "C# Single Responsibility Principle with Dependency Injection"

    • Description: Implement the Single Responsibility Principle with Dependency Injection in C#.
    // Code showing Single Responsibility Principle with Dependency Injection public class Customer { private ILogger logger; public Customer(ILogger logger) { this.logger = logger; } public void AddCustomer() { // Add customer logic logger.Log("Customer added successfully."); } } 
  9. "Benefits of Single Responsibility Principle in C#"

    • Description: Understand the benefits of adhering to the Single Responsibility Principle in C#.
    // Code highlighting benefits of Single Responsibility Principle public class Customer { private ILogger logger; public Customer(ILogger logger) { this.logger = logger; } public void AddCustomer() { // Add customer logic logger.Log("Customer added successfully."); } } 
  10. "C# Single Responsibility Principle in unit testing"

    • Description: Explore how adhering to the Single Responsibility Principle in C# aids in unit testing.
    // Code illustrating Single Responsibility Principle for better unit testing public class Customer { private ILogger logger; public Customer(ILogger logger) { this.logger = logger; } public void AddCustomer() { // Add customer logic logger.Log("Customer added successfully."); } } 

More Tags

mockito pyhive deterministic credentials wget having fastcgi ms-word inorder pymysql

More C# Questions

More Physical chemistry Calculators

More Trees & Forestry Calculators

More Housing Building Calculators

More Entertainment Anecdotes Calculators