-
-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathRepository.cs
193 lines (170 loc) · 7.16 KB
/
Repository.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
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
using Common.Utilities;
using Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
namespace Data.Repositories
{
public class Repository<TEntity> : IRepository<TEntity>
where TEntity : class, IEntity
{
protected readonly ApplicationDbContext DbContext;
public DbSet<TEntity> Entities { get; }
public virtual IQueryable<TEntity> Table => Entities;
public virtual IQueryable<TEntity> TableNoTracking => Entities.AsNoTracking();
public Repository(ApplicationDbContext dbContext)
{
DbContext = dbContext;
Entities = DbContext.Set<TEntity>(); // City => Cities
}
#region Async Method
public virtual ValueTask<TEntity> GetByIdAsync(CancellationToken cancellationToken, params object[] ids)
{
return Entities.FindAsync(ids, cancellationToken);
}
public virtual async Task AddAsync(TEntity entity, CancellationToken cancellationToken, bool saveNow = true)
{
Assert.NotNull(entity, nameof(entity));
await Entities.AddAsync(entity, cancellationToken).ConfigureAwait(false);
if (saveNow)
await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
public virtual async Task AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken, bool saveNow = true)
{
Assert.NotNull(entities, nameof(entities));
await Entities.AddRangeAsync(entities, cancellationToken).ConfigureAwait(false);
if (saveNow)
await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
public virtual async Task UpdateAsync(TEntity entity, CancellationToken cancellationToken, bool saveNow = true)
{
Assert.NotNull(entity, nameof(entity));
Entities.Update(entity);
if (saveNow)
await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
public virtual async Task UpdateRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken, bool saveNow = true)
{
Assert.NotNull(entities, nameof(entities));
Entities.UpdateRange(entities);
if (saveNow)
await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
public virtual async Task DeleteAsync(TEntity entity, CancellationToken cancellationToken, bool saveNow = true)
{
Assert.NotNull(entity, nameof(entity));
Entities.Remove(entity);
if (saveNow)
await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
public virtual async Task DeleteRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken, bool saveNow = true)
{
Assert.NotNull(entities, nameof(entities));
Entities.RemoveRange(entities);
if (saveNow)
await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
#endregion
#region Sync Methods
public virtual TEntity GetById(params object[] ids)
{
return Entities.Find(ids);
}
public virtual void Add(TEntity entity, bool saveNow = true)
{
Assert.NotNull(entity, nameof(entity));
Entities.Add(entity);
if (saveNow)
DbContext.SaveChanges();
}
public virtual void AddRange(IEnumerable<TEntity> entities, bool saveNow = true)
{
Assert.NotNull(entities, nameof(entities));
Entities.AddRange(entities);
if (saveNow)
DbContext.SaveChanges();
}
public virtual void Update(TEntity entity, bool saveNow = true)
{
Assert.NotNull(entity, nameof(entity));
Entities.Update(entity);
if (saveNow)
DbContext.SaveChanges();
}
public virtual void UpdateRange(IEnumerable<TEntity> entities, bool saveNow = true)
{
Assert.NotNull(entities, nameof(entities));
Entities.UpdateRange(entities);
if (saveNow)
DbContext.SaveChanges();
}
public virtual void Delete(TEntity entity, bool saveNow = true)
{
Assert.NotNull(entity, nameof(entity));
Entities.Remove(entity);
if (saveNow)
DbContext.SaveChanges();
}
public virtual void DeleteRange(IEnumerable<TEntity> entities, bool saveNow = true)
{
Assert.NotNull(entities, nameof(entities));
Entities.RemoveRange(entities);
if (saveNow)
DbContext.SaveChanges();
}
#endregion
#region Attach & Detach
public virtual void Detach(TEntity entity)
{
Assert.NotNull(entity, nameof(entity));
var entry = DbContext.Entry(entity);
if (entry != null)
entry.State = EntityState.Detached;
}
public virtual void Attach(TEntity entity)
{
Assert.NotNull(entity, nameof(entity));
if (DbContext.Entry(entity).State == EntityState.Detached)
Entities.Attach(entity);
}
#endregion
#region Explicit Loading
public virtual async Task LoadCollectionAsync<TProperty>(TEntity entity, Expression<Func<TEntity, IEnumerable<TProperty>>> collectionProperty, CancellationToken cancellationToken)
where TProperty : class
{
Attach(entity);
var collection = DbContext.Entry(entity).Collection(collectionProperty);
if (!collection.IsLoaded)
await collection.LoadAsync(cancellationToken).ConfigureAwait(false);
}
public virtual void LoadCollection<TProperty>(TEntity entity, Expression<Func<TEntity, IEnumerable<TProperty>>> collectionProperty)
where TProperty : class
{
Attach(entity);
var collection = DbContext.Entry(entity).Collection(collectionProperty);
if (!collection.IsLoaded)
collection.Load();
}
public virtual async Task LoadReferenceAsync<TProperty>(TEntity entity, Expression<Func<TEntity, TProperty>> referenceProperty, CancellationToken cancellationToken)
where TProperty : class
{
Attach(entity);
var reference = DbContext.Entry(entity).Reference(referenceProperty);
if (!reference.IsLoaded)
await reference.LoadAsync(cancellationToken).ConfigureAwait(false);
}
public virtual void LoadReference<TProperty>(TEntity entity, Expression<Func<TEntity, TProperty>> referenceProperty)
where TProperty : class
{
Attach(entity);
var reference = DbContext.Entry(entity).Reference(referenceProperty);
if (!reference.IsLoaded)
reference.Load();
}
#endregion
}
}