Wednesday, August 2, 2017

SOLID Principles - Single Responsibility Principle

This blog will discuss about the single responsibility principle and will demonstrates how to implement single responsibility principle in C# with example.

The Letter 'S' of 'SOLID' stands for Single Responsibility Principle and The Single Responsibility Principle (SRP) states

"Every class should have a single responsibility",

In different words that a class should have only one reason to change

A single responsibility should be entirely encapsulated by the class so there should be a single reason for making the change to a class and every behavior/function of class should have just one reason to exist. 
For good quality of code the single responsibility principle is essential.

Here is an example of single responsibility principle:

There is an order class, which perform database crud operation like create, update and delete for order entity and also generate details report for order entity now this order class have two responsibilities:
  1. Update/create/delete order record from table 
  2. Generate detail report for Order.
   public class Order
    {

        public int OrderID { getprotected set; }

        public string OrderStatus { getprotected set; }

        public DateTime Date { getprotected set; }

        public int CustomerID { getprotected set; }
        
        /// This method used to add or update order table        
        public void Save(Order Order)

        {


        }


        /// This method used to update the existing order record
        public void Update(Order Order)
        {


        }


        /// This method used to delete the order record from table

        public void Delete(Order Order)
        {


        }


        /// This method used to generate Order details report
        public string GenerateReport(DateTime start, DateTime End)
        {


        }

    }

This class have 4 methods, first 3 methods have common purpose to maintain (create, update or delete) record in order table but GenerateReport() method have completely different purpose to generate OrderReportData Memory string based on input

As per Single responsibility Principle, there should be two separate classes, one to maintain order record and another to generate order report.
  • Order Class-  update/create/delete order record from table 
  • OrderReport Class – Generate order details report
Here is an Order Class:
public class Order
    {
        public int OrderID { getprotected set; }
        public string OrderStatus { getprotected set; }
        public DateTime Date { getprotected set; }
        public int CustomerID { getprotected set; }
        
        /// This method used to add or update order table        
        public void Save(Order Order)

        {


        }


        /// This method used to update the existing order record
        public void Update(Order Order)
        {


        }


        /// This method used to delete the order record from table

        public void Delete(Order Order)
        {


        }

    }

Here is an OrderReport Class - generate order details report.
 public class OrderReport

    {

        
        /// This method used to generate order details reports       
        public string GenerateOrderReport(Order order)

        {


        }

    }



Other related posts:







Thanks for Visiting!!

No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...