I have some C# examples here, which i actually want to know how i can optimize them. I want to cut down the codeexample to the minimum. Is there a practical way to refactor the GetDetails
in the Tour
class or should i refactor it in the CombiTour
class? And of course, how can i refactor it.
public class Tour
{
public string zielID;
public string ort;
public string strasse;
public string nummer;
public string plz;
public string land;
public string name;
public string fahrtNummer;
public Tour(string zielID)
{
this.zielID = zielID;
}
}
public class CombiTour
{
public List<Tour> touren;
public CombiTour()
{
this.touren = new List<Tour>();
}
public void GetDetails()
{
for (int i = 0; i < touren.Count; i++)
{
DataSet dsDetails = Tools.oGenericDs("SELECT Strasse, Nummer, PLZ, Ort, Land, Fahrtnummer FROM Ziele WHERE ZielID = " + this.touren[i].ZielID);
this.touren[i].Strasse = dsDetails.Tables[0].Rows[0]["Strasse"].ToString();
this.touren[i].Nummer = dsDetails.Tables[0].Rows[0]["Nummer"].ToString();
this.touren[i].PLZ = dsDetails.Tables[0].Rows[0]["PLZ"].ToString();
this.touren[i].Ort = dsDetails.Tables[0].Rows[0]["Ort"].ToString();
this.touren[i].Land = dsDetails.Tables[0].Rows[0]["Land"].ToString();
this.touren[i].Fahrtnummer = dsDetails.Tables[0].Rows[0]["Fahrtnummer"].ToString();
dsDetails = Tools.oGenericDs("SELECT Name FROM Kunden WHERE ZielID = " + this.touren[i].ZielID);
this.touren[i].Name = dsDetails.Tables[0].Rows[0]["Name"].ToString();
}
}
}
You could use an ORM (a light one like ORMLite, or a heavyweight one like Entity Framework) and have that do the legwork for you converting data to and from POCOs.
Literally, my code for getting data out of a database using OrmLite is:
var myProducts = connection.Query<Product>("SELECT * FROM Product");
And I get back a collection of products. You don't even need the hand-written SQL (though in more complex cases, you might).
EF also makes heavy use of LinqToSQL, which means you're rarely (if at all) bothering with hand-written SQL and are always dealing with first-class POCOs and LINQ to do all the conversions.