Created
September 25, 2021 06:51
-
-
Save ggMartinez/5df7fe472aa82ac157a60f83857d2301 to your computer and use it in GitHub Desktop.
SOLID principle - Single responsability - Good example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.IO; | |
using System.Threading.Tasks; | |
using MySql.Data.MySqlClient; | |
namespace MyDataManipulationApp{ | |
public class Connection{ | |
private string serverIp; | |
private string databaseName; | |
private string userName; | |
private string userPassword; | |
private MySqlConnection connection; | |
private MySqlCommand query; | |
private MySqlDataReader dataReader; | |
private void connect(){ | |
this.connection = new MySqlConnection( | |
"server=" + this.serverIp + ";" + | |
"userid=" + this.userName + ";" + | |
"password=" + this.userPassword + ";" + | |
"database=" + this.databaseName + ";" | |
); | |
this.connection.open(); | |
this.command = this.connection; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.IO; | |
using System.Threading.Tasks; | |
using MySql.Data.MySqlClient; | |
namespace MyDataManipulationApp{ | |
public class FileWriter{ | |
private string fileName; | |
public void WriteFile(){ | |
this.fileName = @"C:\temp\activeProducts.txt"; | |
Products p = new Products(); | |
foreach(string row in p.GetInformation()){ | |
File.WriteAllText( | |
fileName, | |
row["id"].toString() + | |
row["name"].toString() + | |
row["stock"].toString() + | |
row["description"].toString() + | |
Environment.NewLine | |
); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.IO; | |
using System.Threading.Tasks; | |
using MySql.Data.MySqlClient; | |
namespace MyDataManipulationApp{ | |
public class Products{ | |
// Constructor of the class | |
public void Products(){ | |
this.connect(); | |
} | |
public List<string> GetInformation(){ | |
this.command = "SELECT id,name,stock,description from products WHERE active;"; | |
this.dataReader = this.command.ExecuteReader(); | |
List<string> list = ( | |
from IDataRecord r in this.dataReader | |
select (string)r["FieldName"] | |
).ToList(); | |
return list; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment