lundi 20 avril 2015

populating A Class that has another class as a property

I Have two classes in my windows form application. Let's say Student and StudentDetails where by the class StudentDetails is also a property of Student class.

I Have a stored procedure which gets data from both tables in the database. I need to know how is it that this scenario is usually handled meaning how is Student Class Usually populated. Thanks a lot for your Answers. Any link to a similar scenario wold be great.

here is my code:

 class Student
    {
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public StudentDetails StudentDetails { get; set; }

        public Student()
        {
            this.StudentID = 0;
            this.StudentName = String.Empty;
            this.StudentDetails = new StudentDetails();
        }

    }

class StudentDetails
    {

        public int StudentDetailsID { get; set; }
        public string Address { get; set; }
        public int Height { get; set; }
        public int Weight { get; set; }

        public StudentDetails()
        {
            this.StudentDetailsID = 0;
            this.Address = String.Empty;
            this.Height = 0;
            this.Weight = 0;

        }

    }

The below class has a function that fills the Student Class (FillStudent() )

 Class StudentAssembler
        {

            public List<Model.Student> FillStudent()
            {
                List<Model.Student> StudentCollection = new List<Model.Student>();
                Model.Student StudentDTO = new Model.Student();
                try
                {
                    using (Model.dbConnection dbconnection = new Model.dbConnection(Utilities.SqlConnStr))
                    {

                        SqlDataReader dr;
                        SqlCommand command = new SqlCommand("GetStudents", dbconnection.connection);
                        command.CommandType = CommandType.StoredProcedure;
                        dbconnection.OpenConn();
                        dr = command.ExecuteReader();
                        while (dr.Read())
                        {
                            StudentDTO.StudentID = dr[0] == DBNull.Value ? 0 : (int)dr[0];
                            StudentDTO.StudentName = dr[1] == DBNull.Value ? String.Empty : (String)dr[1];

//this is the part that i need to know how is it usually handled

//StudentDTO.StudentDetails = how to fill out this part ???
                                StudentCollection.Add(StudentDTO);
                            }

                            return StudentCollection;
                        }
                    }
                    catch (Exception)
                    {
                        return null;
                    }
                }
            }

Aucun commentaire:

Enregistrer un commentaire