-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSP_Bad.cs
82 lines (80 loc) · 1.87 KB
/
LSP_Bad.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System.Text;
namespace SOLID_Principles.LSP.Bad
{
public class SqlFile
{
public string FilePath { get; set; }
public string FileText { get; set; }
public string LoadText()
{
/* Code to read text from sql file */
return "";
}
public string SaveText()
{
/* Code to save text into sql file */
return "";
}
}
public class SqlFileManager
{
public List<SqlFile> lstSqlFiles { get; set; }
public string GetTextFromFiles()
{
StringBuilder objStrBuilder = new StringBuilder();
foreach (var objFile in lstSqlFiles)
{
objStrBuilder.Append(objFile.LoadText());
}
return objStrBuilder.ToString();
}
public void SaveTextIntoFiles()
{
foreach (var objFile in lstSqlFiles)
{
objFile.SaveText();
}
}
}
/// <summary>
/// a few read-only files in the application folder, so we need to restrict the flow whenever it tries to do a save on them.
/// </summary>
public class ReadOnlySqlFile : SqlFile
{
public string FilePath { get; set; }
public string FileText { get; set; }
public string LoadText()
{
/* Code to read text from sql file */
return "";
}
public void SaveText()
{
/* Throw an exception when app flow tries to do save. */
throw new IOException("Can't Save");
}
}
public class SqlFileManager_AvoidExeception
{
public List<SqlFile> lstSqlFiles { get; set; }
public string GetTextFromFiles()
{
StringBuilder objStrBuilder = new StringBuilder();
foreach (var objFile in lstSqlFiles)
{
objStrBuilder.Append(objFile.LoadText());
}
return objStrBuilder.ToString();
}
public void SaveTextIntoFiles()
{
foreach (var objFile in lstSqlFiles)
{
//Check whether the current file object is read-only or not.If yes, skip calling it's
// SaveText() method to skip the exception.
if (objFile is not ReadOnlySqlFile)
objFile.SaveText();
}
}
}
}