Skip to content

Instantly share code, notes, and snippets.

@ggMartinez
Last active September 25, 2021 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggMartinez/c0c703269d407aedf64bb431d374b0aa to your computer and use it in GitHub Desktop.
Save ggMartinez/c0c703269d407aedf64bb431d374b0aa to your computer and use it in GitHub Desktop.
SOLID principle - Single responsability - Bad example
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 DataManipulation{
private string serverIp;
private string databaseName;
private string userName;
private string userPassword;
private MySqlConnection connection;
private MySqlCommand query;
private MySqlDataReader dataReader;
private string fileName;
// Use the constructor to connect to the database
public void DataManipulation(){
this.connect();
}
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;
}
private void disconnect(){
this.connection.close();
}
private List<string> ReadInformation(){
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;
}
public void WriteFile(){
this.fileName = @"C:\temp\activeProducts.txt";
foreach(string row in this.ReadInformation){
File.WriteAllText(
fileName,
row["id"].toString() +
row["name"].toString() +
row["stock"].toString() +
row["description"].toString() +
Environment.NewLine
);
}
this.disconnect();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment