Hollo my friends
My question is all about class design.
I have this app that is connecting to a SQL server, retrieving some tables, the user works on the data, and then it updates the data base.
I should say that all that i know i learned on my own (youtube and reading) but there's a lot of stuff that makes me confuse though.
So for what i have been seen, the best approach for my project is to create a class that encapsulates (nice word by the way) this model i just discribed.
I allready have a version working very fine, but the code it's all merge into the interface layer. (By working very fine i mean. I already can retrieve the data from the database, i then can work on the tables, and then i can update the database too)
But i fell like i'm acheving this in a very rudimentar way. Now i'm trying to make it more harmonious and well disign. Trying to master my code a bit more i guess.
So here is what i achieve untill now based on the "Class" theory and OOP.
Could You help me? Tell me if i'm doing it good? I'm not sure if i'm designing my class in the proper way.
So here is the new version that i'm making now. (Sorry if it's too big)
// Main class that i'm trying to model in such way to be a conceptual model of my real SQL Database (FootballProject Database).
public class fp_Database
{
// Virtual DB.
public static DataSet DS = new DataSet();
// Construtor.
public fp_Database()
{
DS.Tables.Add("Seassons");
DS.Tables.Add("Countries");
DS.Tables.Add("Tournaments");
DS.Tables.Add("TeamsAllowed");
DS.Tables.Add("Matches");
Tables = new fp_Tables();
}
// Public Object fp_Tables containing the fp_Database tables.
public fp_Tables Tables;
// fp_Tables Class definition.
public class fp_Tables
{
// Instantiating all the Databases tables.
public tableMatches Matches = new tableMatches();
public tableSeassons Seassons = new tableSeassons();
public tableCountries Countries = new tableCountries();
public tableTournaments Tournaments = new tableTournaments();
// Definition of all the fp_Tables Classes.
public class tableSeassons
{
public DataTable Get_Table
{
get
{
return DS.Tables["Seassons"];
}
}
public tableSeassons()
{
DatabaseInterface DBInterface = new DatabaseInterface();
SqlCommand CMD = new SqlCommand("SELECT * FROM fp.Seassons");
DBInterface.GetTable(DS.Tables["Seassons"], CMD);
}
}
public class tableCountries
{
public DataTable Get_Table
{
get
{
return DS.Tables["Countries"];
}
}
public tableCountries()
{
DatabaseInterface DBInterface = new DatabaseInterface();
SqlCommand CMD = new SqlCommand("SELECT * FROM fp.Countries");
DBInterface.GetTable(DS.Tables["Countries"], CMD);
}
}
public class tableTournaments
{
public DataTable Get_Table
{
get
{
return DS.Tables["Tournaments"];
}
}
public void Set_Filters(int Seasson, int Country)
{
DS.Tables["Tournaments"].DefaultView.RowFilter = "Seasson = " + Seasson + " AND Country = " + Country;
}
public tableTournaments()
{
DatabaseInterface DBInterface = new DatabaseInterface();
SqlCommand CMD = new SqlCommand("SELECT * FROM fp.Tournaments ORDER BY Tournament");
DBInterface.GetTable(DS.Tables["Tournaments"], CMD);
}
}
public class tableMatches
{
public DataTable Get_Table
{
get
{
return DS.Tables["Matches"];
}
}
public void Load_Table_By_Tournament(int TournamentID)
{
DatabaseInterface DBInterface = new DatabaseInterface();
SqlCommand CMD = new SqlCommand("SELECT * FROM fp.Matches WHERE Tournament = " + TournamentID);
DS.Tables["Matches"].Clear();
DBInterface.GetTable(DS.Tables["Matches"], CMD);
}
}
}
}
// DATA ACCESS LAYER. (SELECT - INSERT - UPDATE - DELETE)
class DatabaseInterface
{
// Latter i will learn how to save the connection string into app.config file and read from int too.
private SqlConnection Conn = new SqlConnection("Data Source=DESKTOP-8MGVKID\\MSSQLSERVER2016;Initial Catalog=FootballProject;Integrated Security=True");
// Select Method.
public void GetTable (DataTable Table, SqlCommand SQLCommand)
{
SqlDataAdapter DA = new SqlDataAdapter(SQLCommand.CommandText, Conn);
Conn.Open();
DA.Fill(Table);
Conn.Close();
}
// Here will be Insert Method
// Here will be Update Method.
// Here will be Delete Method
}
Aucun commentaire:
Enregistrer un commentaire