Skip to content

Instantly share code, notes, and snippets.

@ggMartinez
Created September 25, 2021 06:51
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/5df7fe472aa82ac157a60f83857d2301 to your computer and use it in GitHub Desktop.
Save ggMartinez/5df7fe472aa82ac157a60f83857d2301 to your computer and use it in GitHub Desktop.
SOLID principle - Single responsability - Good 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 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;
}
}
}
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
);
}
}
}
}
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