-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelEntity.cs
More file actions
240 lines (218 loc) · 9.05 KB
/
ModelEntity.cs
File metadata and controls
240 lines (218 loc) · 9.05 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Threading.Tasks;
using yourapp.persistance;
namespace UM4RS.persistance
{
/// <summary>
/// Generic Class for Model Objects.
/// This class has the CRUD basic operations
/// </summary>
/// <typeparam name="TObject"></typeparam>
public abstract class ModelEntity<TObject> where TObject : class, IBaseEntity
{
static readonly YourContext ModelContext = new YourContext();
#region -- CLASS METHODS --
/// <summary>
/// Returns a single object with a primary key of the provided id
/// </summary>
/// <remarks>Synchronous</remarks>
/// <param name="id">The primary key of the object to fetch</param>
/// <returns>A single object with the provided primary key or null</returns>
public static TObject Get(int id)
{
return ModelContext.Set<TObject>().Find(id);
}
/// <summary>
/// Returns a single object with a primary key of the provided id
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <param name="id">The primary key of the object to fetch</param>
/// <returns>A single object with the provided primary key or null</returns>
public static async Task<TObject> GetAsync(int id)
{
return await ModelContext.Set<TObject>().FindAsync(id);
}
/// <summary>
/// Gets a collection of all objects in the database
/// </summary>
/// <remarks>Synchronous</remarks>
/// <returns>An ICollection of every object in the database</returns>
public static ICollection<TObject> GetAll()
{
return ModelContext.Set<TObject>().ToList();
}
/// <summary>
/// Gets a collection of all objects in the database
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <returns>An ICollection of every object in the database</returns>
public static async Task<ICollection<TObject>> GetAllAsync()
{
return await ModelContext.Set<TObject>().ToListAsync();
}
/// <summary>
/// Returns a single object which matches the provided expression
/// </summary>
/// <remarks>Synchronous</remarks>
/// <param name="match">A Linq expression filter to find a single result</param>
/// <returns>A single object which matches the expression filter.
/// If more than one object is found or if zero are found, null is returned</returns>
public static TObject Find(Expression<Func<TObject, bool>> match)
{
return ModelContext.Set<TObject>().SingleOrDefault(match);
}
/// <summary>
/// Returns a single object which matches the provided expression
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <param name="match">A Linq expression filter to find a single result</param>
/// <returns>A single object which matches the expression filter.
/// If more than one object is found or if zero are found, null is returned</returns>
public static async Task<TObject> FindAsync(Expression<Func<TObject, bool>> match)
{
return await ModelContext.Set<TObject>().SingleOrDefaultAsync(match);
}
/// <summary>
/// Returns a collection of objects which match the provided expression
/// </summary>
/// <remarks>Synchronous</remarks>
/// <param name="match">A linq expression filter to find one or more results</param>
/// <returns>An ICollection of object which match the expression filter</returns>
public static ICollection<TObject> FindAll(Expression<Func<TObject, bool>> match)
{
return ModelContext.Set<TObject>().Where(match).ToList();
}
/// <summary>
/// Returns a collection of objects which match the provided expression
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <param name="match">A linq expression filter to find one or more results</param>
/// <returns>An ICollection of object which match the expression filter</returns>
public static async Task<ICollection<TObject>> FindAllAsync(Expression<Func<TObject, bool>> match)
{
return await ModelContext.Set<TObject>().Where(match).ToListAsync();
}
/// <summary>
/// Saves a new or modified object into the database and commits the change
/// </summary>
/// <remarks>Synchronous</remarks>
/// <param name="t">The object to Save</param>
/// <returns>The resulting object including its primary key after the Save</returns>
public static TObject Save(TObject t)
{
if (t == null) return null;
if (t.Id > 0)
{
// update the object
ModelContext.Entry(t).State = EntityState.Modified;
}
else
{
// add the new object
ModelContext.Set<TObject>().Add(t);
}
ModelContext.SaveChanges();
return t;
}
/// <summary>
/// Inserts a single object to the database and commits the change
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <param name="t">The object to insert</param>
/// <returns>The resulting object including its primary key after the insert</returns>
public static async Task<TObject> SaveAsync(TObject t)
{
if (t == null) return null;
if (t.Id > 0)
{
// update the object
ModelContext.Entry(t).State = EntityState.Modified;
}
else
{
// add the new object
ModelContext.Set<TObject>().Add(t);
}
await ModelContext.SaveChangesAsync();
return t;
}
/// <summary>
/// Inserts a collection of objects into the database and commits the changes
/// </summary>
/// <remarks>Synchronous</remarks>
/// <param name="tList">An IEnumerable list of objects to insert</param>
/// <returns>The IEnumerable resulting list of inserted objects including the primary keys</returns>
public static IEnumerable<TObject> SavedAll(IEnumerable<TObject> tList)
{
ModelContext.Set<TObject>().AddRange(tList);
ModelContext.SaveChanges();
return tList;
}
/// <summary>
/// Inserts a collection of objects into the database and commits the changes
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <param name="tList">An IEnumerable list of objects to insert</param>
/// <returns>The IEnumerable resulting list of inserted objects including the primary keys</returns>
public static async Task<IEnumerable<TObject>> SaveAllAsync(IEnumerable<TObject> tList)
{
ModelContext.Set<TObject>().AddRange(tList);
await ModelContext.SaveChangesAsync();
return tList;
}
/// <summary>
/// Deletes a single object from the database and commits the change
/// </summary>
/// <remarks>Synchronous</remarks>
/// <param name="t">The object to delete</param>
public static void Delete(TObject t)
{
ModelContext.Set<TObject>().Remove(t);
ModelContext.SaveChanges();
}
/// <summary>
/// Deletes a single object from the database and commits the change
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <param name="t">The object to delete</param>
public async Task<int> DeleteAsync(TObject t)
{
ModelContext.Set<TObject>().Remove(t);
return await ModelContext.SaveChangesAsync();
}
/// <summary>
/// Gets the count of the number of objects in the database
/// </summary>
/// <remarks>Synchronous</remarks>
/// <returns>The count of the number of objects</returns>
public static int Count()
{
return ModelContext.Set<TObject>().Count();
}
/// <summary>
/// Gets the count of the number of objects in the database
/// </summary>
/// <remarks>Asynchronous</remarks>
/// <returns>The count of the number of objects</returns>
public static async Task<int> CountAsync()
{
return await ModelContext.Set<TObject>().CountAsync();
}
#endregion
#region -- INSTANCE METHODS --
/// <summary>
/// Save the current objecto to database
/// </summary>
public void Save()
{
var baseEntity = this as TObject;
Debug.Assert(baseEntity != null);//assert that its not violated
ModelContext.Set<TObject>().Add(baseEntity);
ModelContext.SaveChanges();
}
#endregion
}
}