using System; using System.Collections.Generic; using System.Linq; using SchoolSample.EntityModel; using System.Data; namespace SchoolSample.Wcf.Service { public class SchoolService : ISchoolService { public List<Student> GetStudents(string includeOption) { using (var context = new SchoolEntities()) { switch (includeOption) { case "None": return context.People.OfType<Student>() .ToList(); case "Enrollments.Course": var studentList = new List<Student>(); foreach (var student in context.People.OfType<Student>()) { var currentStudent = student; using (var innerContext = new SchoolEntities()) { studentList.Add( innerContext.People.OfType<Student>() .Include("Enrollments.Course") .Single(n => n.PersonId == currentStudent.PersonId)); } } return studentList; default: throw new NotImplementedException(); } } } public Student GetStudentById(int studentId) { using (var context = new SchoolEntities()) { return context.People.OfType<Student>() .Include("Enrollments.Course") .FirstOrDefault(n => n.PersonId == studentId); } } public List<Instructor> GetInstructors(string includeOption) { using (var context = new SchoolEntities()) { switch (includeOption) { case "None": return context.People.OfType<Instructor>() .ToList(); case "Courses": return context.People.OfType<Instructor>() .Include("Courses") .ToList(); default: throw new NotImplementedException(); } } } public Instructor GetInstructorById(int instructorId) { using (var context = new SchoolEntities()) { return context.People.OfType<Instructor>() .Include("Courses") .FirstOrDefault(n => n.PersonId == instructorId); } } public List<Course> GetCourses() { using (var context = new SchoolEntities()) { var courseList = new List<Course>(); foreach (var course in context.Courses) { var currentCourse = course; using (var innerContext = new SchoolEntities()) { courseList.Add( innerContext.Courses .Include("Enrollments.Student") .Single(n => n.CourseId == currentCourse.CourseId)); } } return courseList; } } public Course GetCourseById(int courseId) { using (var context = new SchoolEntities()) { return context.Courses .Include("Enrollments.Student") .FirstOrDefault(n => n.CourseId == courseId); } } public List<object> UpdateInstructor(Instructor item) { var returnList = new List<object>(); try { using (var context = new SchoolEntities()) { switch (item.ChangeTracker.State) { case ObjectState.Added: // server side validation item.ValidateObjectGraph(); // save changes context.People.ApplyChanges(item); context.SaveChanges(); break; case ObjectState.Deleted: // verify whether there is any course assigned to this instructor var courseExists = context.Courses.Any(n => n.InstructorId == item.PersonId); if (courseExists) { returnList.Add("Cannot delete, there still exists course assigned to this instructor."); returnList.Add(item.PersonId); return returnList; } // save changes context.People.ApplyChanges(item); context.SaveChanges(); break; default: // server side validation item.ValidateObjectGraph(); // save changes context.People.ApplyChanges(item); context.SaveChanges(); break; } } returnList.Add(string.Empty); returnList.Add(item.PersonId); } catch (OptimisticConcurrencyException) { var errorMessage = "Instructor " + item.PersonId + " was modified by another user. " + "Refresh that item before reapply your changes."; returnList.Add(errorMessage); returnList.Add(item.PersonId); } catch (Exception ex) { Exception exception = ex; while (exception.InnerException != null) { exception = exception.InnerException; } var errorMessage = "Instructor " + item.PersonId + " has error: " + exception.Message; returnList.Add(errorMessage); returnList.Add(item.PersonId); } return returnList; } public List<object> UpdateStudent(Student item) { var returnList = new List<object>(); try { using (var context = new SchoolEntities()) { switch (item.ChangeTracker.State) { case ObjectState.Added: // server side validation item.ValidateObjectGraph(); // save changes context.People.ApplyChanges(item); context.SaveChanges(); break; case ObjectState.Deleted: // verify whether there is any course enrollment for this student var courseExists = context.Enrollments.Any(n => n.StudentId == item.PersonId); if (courseExists) { returnList.Add("Cannot delete, there still exists course enrollment for this student."); returnList.Add(item.PersonId); return returnList; } // save changes context.People.ApplyChanges(item); context.SaveChanges(); break; default: // server side validation item.ValidateObjectGraph(); // save changes context.People.ApplyChanges(item); context.SaveChanges(); break; } } returnList.Add(string.Empty); returnList.Add(item.PersonId); } catch (OptimisticConcurrencyException) { var errorMessage = "Student " + item.PersonId + " was modified by another user. " + "Refresh that item before reapply your changes."; returnList.Add(errorMessage); returnList.Add(item.PersonId); } catch (Exception ex) { Exception exception = ex; while (exception.InnerException != null) { exception = exception.InnerException; } var errorMessage = "Student " + item.PersonId + " has error: " + exception.Message; returnList.Add(errorMessage); returnList.Add(item.PersonId); } return returnList; } public List<object> UpdateCourse(Course item) { var returnList = new List<object>(); try { using (var context = new SchoolEntities()) { switch (item.ChangeTracker.State) { case ObjectState.Added: // server side validation item.ValidateObjectGraph(); // save changes context.Courses.ApplyChanges(item); context.SaveChanges(); break; case ObjectState.Deleted: // save changes context.Courses.ApplyChanges(item); context.SaveChanges(); break; default: // server side validation item.ValidateObjectGraph(); // save changes context.Courses.ApplyChanges(item); context.SaveChanges(); break; } } returnList.Add(string.Empty); returnList.Add(item.CourseId); } catch (OptimisticConcurrencyException) { var errorMessage = "Course " + item.CourseId + " was modified by another user. " + "Refresh that item before reapply your changes."; returnList.Add(errorMessage); returnList.Add(item.CourseId); } catch (Exception ex) { Exception exception = ex; while (exception.InnerException != null) { exception = exception.InnerException; } var errorMessage = "Course " + item.CourseId + " has error: " + exception.Message; returnList.Add(errorMessage); returnList.Add(item.CourseId); } return returnList; } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)