I have a question on the correct way to assign values obtained from the database in the data access layer to the data contracts at the WCF layer.
Consider a simple scenario in which to get a list of all students from the database and then assign it to a student data contract.
My student data contract looks like:
[DataContract]
public class StudentDC
{
[DataMember]
public int StudentID { get; set; }
[DataMember]
public string StudentName { get; set; }
}
In my data access class, I would have a method like List GetAllStudents()
My question is on the implementation of my GetAllStudents() method. Is it ok for it to look like the following:
public List GetAllStudents() {
List<StudentDC> studentDCs = new List<StudentDC>(); var students = db.Students_TB.Select(s => s).ToList(); foreach(Student_TB student in students) { StudentDC studentDC = new StudentDC(); studentDC.StudentID = student.StudentID; studentDC.StudentName = student.StudentName; studentDCs.Add(studentDC); } return studentDCs;
}
Is the above way of assigning values to the data contracts correct, or should I have student business object classes to accept the data in the data access layer, and then pass on the values to the data contracts in the service contract implementation, like the following:
I would have student business object classes like the following:
public class StudentBO {
int studentID; string studentName; public int StudentID { get { return studentID; } set { studentID = value; } } public <return type> BusinessLogicMethod1() { // Implementation }
}
In the data access layer, I would then assign the values got from the database to a collection of student business objects as folows:
public List<StudentBO> GetAllStudents()
{
List<StudentBO> studentBOs = new List<StudentBO>();
var students = db.Students_TB.Select(s => s).ToList();
foreach(Student_TB student in students)
{
StudentBO studentBO = new StudentBO();
studentBO.StudentID = student.StudentID;
studentBO.StudentName = student.StudentName;
studentBOs.Add(studentBO);
}
return studentBOs;
}
The values in the student business objects would then be assigned to the student data contracts in the service contract implementation, from where it would be sent out over the wire.
Which of the two ways above is the correct way?