0

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();
        }
    }
}
4

1 回答 1

2

您可以使用 ORM(一种轻量级的ORMLite或重量级的Entity Framework)并让您完成与 POCO 之间的数据转换工作。

从字面上看,我使用 OrmLite 从数据库中获取数据的代码是:

var myProducts = connection.Query<Product>("SELECT * FROM Product");

我拿回了一系列产品。您甚至不需要手写 SQL(尽管在更复杂的情况下,您可能会这样做)。

EF 还大量使用 LinqToSQL,这意味着您很少(如果有的话)为手写 SQL 而烦恼,并且总是使用一流的 POCO 和 LINQ 来完成所有转换。

于 2013-11-13T11:26:54.023 回答