-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCourseProvider.cs
164 lines (145 loc) · 4.03 KB
/
CourseProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System.Collections.Generic;
using ClassLibrary;
using NHibernate;
using NHibernate.Criterion;
namespace DataAccessLayer {
/// <summary>
///
/// </summary>
public class CourseProvider : GenericDataProvider<Course> {
#region Constructors
/// <summary>
/// Initializes a new instance of the CourseProvider class.
/// </summary>
public CourseProvider()
{
}
/// <summary>
/// Initializes a new instance of the CourseProvider class.
/// </summary>
/// <param name="session"></param>
public CourseProvider(ISession session)
: base(session) {
}
#endregion
#region Public Methods
/// <summary>
/// Adds the course.
/// </summary>
/// <param name="course">The course.</param>
/// <returns></returns>
/// <exception cref="HibernateException"><c>HibernateException</c>.</exception>
public int AddCourse(Course course) {
course.Validate();
using (ITransaction tx = Session.BeginTransaction()) {
try {
var newID = (int)Session.Save(course);
Session.Flush();
tx.Commit();
return newID;
} catch (HibernateException) {
tx.Rollback();
throw;
}
}
}
/// <summary>
/// Deletes the course.
/// </summary>
/// <param name="course">The course.</param>
/// <exception cref="HibernateException"><c>HibernateException</c>.</exception>
public void DeleteCourse(Course course) {
using (ITransaction tx = Session.BeginTransaction()) {
try {
Session.Delete(course);
Session.Flush();
tx.Commit();
} catch (HibernateException) {
tx.Rollback();
throw;
}
}
}
/// <summary>
/// Gets all courses.
/// </summary>
/// <returns></returns>
public IList<Course> GetAllCourses()
{
DetachedCriteria allCourses = DetachedCriteria.For<Course>()
.AddOrder(new Order("CourseName", true))
.SetFetchMode("AssignedProfessor", FetchMode.Eager)
.SetFetchMode("ProfessorsAssociatedList", FetchMode.Eager)
.SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer());
return FindByDetachedCriteria(allCourses);
}
/// <summary>
/// Gets the course by ID.
/// </summary>
/// <param name="id">The id.</param>
/// <returns></returns>
public Course GetCourseByID(int id) {
return GetById(id);
}
/// <summary>
/// Gets the non nullable prof ID.
/// </summary>
/// <returns></returns>
public IList<Course> GetNonNullableProfID() {
DetachedCriteria nonNullableProfs = DetachedCriteria.For<Course>()
.Add(Restrictions.IsNotNull("AssignedProfessor"));
return FindByDetachedCriteria(nonNullableProfs);
//return GetCoursesByQuery("select from Course c where c.AssignedProfessor is not null");
}
/// <summary>
/// Gets the nullable prof ID.
/// </summary>
/// <returns></returns>
public IList<Course> GetNullableProfID() {
DetachedCriteria nullableProfs = DetachedCriteria.For<Course>()
.Add(Restrictions.IsNull("AssignedProfessor"));
return FindByDetachedCriteria(nullableProfs);
//return GetCoursesByQuery("select from Course c where c.AssignedProfessor is null");
}
/// <summary>
/// Saves the or update courses.
/// </summary>
/// <param name="courses">The courses.</param>
/// <exception cref="HibernateException"><c>HibernateException</c>.</exception>
public void SaveOrUpdateCourses(IList<Course> courses) {
using (ITransaction tx = Session.BeginTransaction()) {
try {
foreach (Course c in courses) {
Session.SaveOrUpdate(c);
}
Session.Flush();
tx.Commit();
} catch (HibernateException) {
tx.Rollback();
throw;
}
}
}
/// <summary>
/// Updates the course.
/// </summary>
/// <param name="course">The course.</param>
/// <exception cref="HibernateException"><c>HibernateException</c>.</exception>
public void UpdateCourse(Course course) {
course.Validate();
using (ITransaction tx = Session.BeginTransaction()) {
try
{
Session.Update(course);
tx.Commit();
}
catch (HibernateException)
{
tx.Rollback();
throw;
}
}
}
#endregion
}
}