public HttpResponseMessage Post(StudentModel studentModel) { Student student = ModelsBridge.SerilizeStudent(studentModel); using (this.unitOfWork = new UnitOfWorkScope(true)) { UnitOfWorkScope.StudentRepository.Add(student); } return this.Request.CreateResponse(HttpStatusCode.OK, student); }
public UnitOfWorkScope(bool toSaveAllChanges) { if ((currScope != null) && (!currScope.isDisposed)) { throw new InvalidOperationException(String.Format("Nestig of contexts is not allowed!")); } this.toSaveAllChanges = toSaveAllChanges; this.context = new SchoolDbContext(); this.isDisposed = false; Thread.BeginThreadAffinity(); currScope = this; }
public void Dispose() { if (!this.isDisposed) { currScope = null; Thread.EndThreadAffinity(); if (this.ToSaveAllChanges) { this.context.SaveChanges(); } this.context.Dispose(); this.isDisposed = true; } }
public HttpResponseMessage GetAll() { ICollection<Student> all = null; ICollection<StudentModel> models = null; using (this.unitOfWork = new UnitOfWorkScope(true)) { all = UnitOfWorkScope.StudentRepository.GetAll().ToList(); } models = new HashSet<StudentModel>(); foreach (var student in all) { models.Add(ModelsBridge.SerilizeStudentModel(student)); } return this.Request.CreateResponse(HttpStatusCode.OK, models); }