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;
}
}
- Insert data into the database
- 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);
}
}