views:

127

answers:

2

How to encapsulate below codes (i need refoctoring)

    public class mycollection
{
        private DateTime tarih;
        private int sira;
        private int visitingcount;

        public DateTime TARIH { get { return tarih; } set { tarih = value; } }
        public int SIRA { get { return sira; } set { sira = value; } }
        public int VISITINGCOUNT { get { return visitingcount; } set { visitingcount = value; } }
}

i used this class below

DataRow[] rows = dsChart.Tables[0].Select("TARIH>='" + DateGap1 + "' and TARIH<='" + DateGap2 + "'");

                                list = new List<mycollection>();
                                foreach (DataRow dr in rows)
                                {
                                    mycollection mycol = new mycollection();
                                    mycol.TARIH = Convert.ToDateTime(dr["TARIH"].ToString());
                                    mycol.SIRA = Convert.ToInt32(dr["SIRA"].ToString());
                                    mycol.VISITINGCOUNT = Convert.ToInt32(dr["VISITINGCOUNT"].ToString());
                                    list.Add(mycol);
                                }

i need like that :

  public static void LoadDepartman(string departmanName)
        {
            List<StuffDepartman> li = new List<StuffDepartman>();
            GetDepartman departman = new GetDepartman();
            DataSet ds = departman.GetDepartmanA(departmanName);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                li.Add(new StuffDepartman
                {
                    Name = dr["Name"].ToString(),
                    SurName = dr["SurName"].ToString(),
                    Address = dr["Address"].ToString(),
                    Phone = dr["Phone"].ToString(),
                    DepartmanName = dr["DepartmanName"].ToString(),
                    Salary =int.Parse( dr["Salary"].ToString()),
                    Married = dr["Married"].ToString()
                }
                );
            }
            HttpContext.Current.Session["Stuffs"] = li;

        }
+1  A: 

(I don't fully understand how the first two examples relate to the third... they don't seem to share anything in common)

So what currently breaks? The biggest issue I can see is that (IIRC) to be reliably usable with Session (with different back end implementations) the item must be serializable (var BinaryFormatter), but that may be as simple as marking mycollection with [Serializable].

For info mycollection is a confusing name for something that is a row entity, not the actual collection - and if you have C# 3.0 you could simplify things a bit with automattically implemented properties:

[Serializable]
public class SomeSensibleName
{
    public DateTime Tarih {get;set;}
    public int Sira {get;set;}
    public int VisitCount {get;set;}
}

However; it isn't clear (from your code) whether this will change anything. Please can you clarify what is currently happening?

Marc Gravell
Very useful information +1
ichiban
+1  A: 

If I understand correctly, this is the refactoring you are looking for:

DataRow[] rows = dsChart.Tables[0].Select("TARIH >='" + DateGap1 + "' and TARIH <='" + DateGap2 + "'");
List<mycollection> list = new List<mycollection>();
foreach (DataRow dr in rows)
{
    list.Add(new mycollection
    { 
      TARIH = Convert.ToDateTime(dr["TARIH"].ToString());
      SIRA = Convert.ToInt32(dr["SIRA"].ToString());
      VISITINGCOUNT = Convert.ToInt32(dr["VISITINGCOUNT"].ToString());
    });
}

Keep in mind that in order for this to work you need to be running the .NET Framework 3.x (VS2008).

Jose Basilio
That's what I understood from the question as well +1
ichiban