コード例 #1
0
        // GET api/students
        public HttpResponseMessage GetStudents()
        {
            // My original idea was to create 2 different data access layers (one with entity framework and one with nHibernate),
            // then let you use a Header value to determine which DAL to use and inject the chosen provider into the service.
            using (provider = new EntDAL.EFProvider())
            {
                StudentService service = new StudentService(provider);

                List<StudentDTO> students = service.GetStudents();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, students, "application/json");

                return response;
            }
        }
コード例 #2
0
        // GET api/students/1
        public StudentDTO GetStudent(int id)
        {
            using (provider = new EntDAL.EFProvider())
            {
                StudentService service = new StudentService(provider);

                StudentDTO student = service.GetStudent(id);

                if (student == null)
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
                }

                // this should still return the proper response headers
                // even without specifically returning an HttpResponseMessage object
                return student;
            }
        }
コード例 #3
0
        // DELETE api/students/5
        public HttpResponseMessage Delete(int id)
        {
            using (provider = new EntDAL.EFProvider())
            {
                StudentService service = new StudentService(provider);

                if (service.DeleteStudent(id))
                {
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.NoContent);
                    return response;
                }
                else
                {
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.NotFound);
                    return response;
                }
            }
        }
コード例 #4
0
        // POST api/Students
        public HttpResponseMessage PostStudent(Student student)
        {
            if (ModelState.IsValid)
            {
                using (provider = new EntDAL.EFProvider())
                {
                    StudentService service = new StudentService(provider);

                  StudentDTO stud =  service.CreateStudent(student);

                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, stud);
                    return response;
                }
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }