Articles → .NET DESIGN PATTERN → Single Responsibility Principle In C#

Single Responsibility Principle In C#






Purpose





Example




public class Database
{
        public int InsertData(string query)
        {
            try
            {
                //  LOGIC TO INSERT DATA
            }
            catch (Exception ex)
            {
                File.AppendAllText(@"c:\log\error.txt", ex.Message);
            }

            return 1;
        }
}




  1. Insert data into the database
  2. Logging an error


public class Database
 {
        public int InsertData(string query)
        {
            try
            {
                //  LOGIC TO INSERT DATA
            }
            catch (Exception ex)
            {
                var logger = new FileLogger();
                logger.LogError(ex.Message);
            }

            return 1;
        }
}

public class FileLogger
{
        public void LogError(string error)
        {
            File.AppendAllText(@"c:\log\error.txt", error);
        }
}





Posted By  -  Karan Gupta
 
Posted On  -  Monday, December 28, 2020
 
Updated On  -  Tuesday, August 16, 2022

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250