Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5. using System.Reflection;
  6.  
  7. namespace ExercisesDAL
  8. {
  9. public class StudentModel
  10. {
  11. IRepository<Students> repository;
  12.  
  13. public StudentModel()
  14. {
  15. repository = new SomeSchoolRepository<Students>();
  16. }
  17.  
  18. public Students GetByLastName(string name)
  19. {
  20. List<Students> selectedStudent = null;
  21.  
  22. try
  23. {
  24. selectedStudent = repository.GetByExpression(stu => stu.LastName == name);
  25. }
  26. catch (Exception ex)
  27. {
  28. Console.WriteLine("Problem in " + GetType().Name + " " +
  29. MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  30. throw ex;
  31. }
  32.  
  33. return selectedStudent.FirstOrDefault();
  34. }
  35.  
  36. public Students GetById(int id)
  37. {
  38. List<Students> selectedStudent = null;
  39.  
  40. try
  41. {
  42. selectedStudent = repository.GetByExpression(stu => stu.Id == id);
  43. }
  44. catch (Exception ex)
  45. {
  46. Console.WriteLine("Problem in " + GetType().Name + " " +
  47. MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  48. throw ex;
  49. }
  50.  
  51. return selectedStudent.FirstOrDefault();
  52. }
  53.  
  54. public List<Students> GetAll()
  55. {
  56. List<Students> allStudents = new List<Students>();
  57.  
  58. try
  59. {
  60. allStudents = repository.GetAll();
  61. }
  62. catch (Exception ex)
  63. {
  64. Console.WriteLine("Problem in " + GetType().Name + " " +
  65. MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  66. throw ex;
  67. }
  68.  
  69. return allStudents;
  70. }
  71.  
  72. public int Add(Students newStudent)
  73. {
  74. try
  75. {
  76. repository.Add(newStudent);
  77. }
  78. catch(Exception ex)
  79. {
  80. Console.WriteLine("Problem in " + GetType().Name + " " +
  81. MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  82. throw ex;
  83. }
  84.  
  85. return newStudent.Id;
  86. }
  87.  
  88. public UpdateStatus Update(Students updatedStudent)
  89. {
  90. UpdateStatus operationStatus = UpdateStatus.Failed;
  91.  
  92. try
  93. {
  94. operationStatus = repository.Update(updatedStudent);
  95. }
  96. catch (Exception ex)
  97. {
  98. Console.WriteLine("Problem in " + GetType().Name + " " +
  99. MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  100. throw ex;
  101. }
  102.  
  103. return operationStatus;
  104. }
  105.  
  106. public int Delete(int Id)
  107. {
  108. int studentsDeleted = -1;
  109.  
  110. try
  111. {
  112. studentsDeleted = repository.Delete(Id);
  113. }
  114. catch (Exception ex)
  115. {
  116. Console.WriteLine("Problem in " + GetType().Name + " " +
  117. MethodBase.GetCurrentMethod().Name + " " + ex.Message);
  118. throw ex;
  119. }
  120.  
  121. return studentsDeleted;
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement