-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
EfRepositoryBaseOfTEntityAndTPrimaryKey.cs
249 lines (205 loc) · 7.07 KB
/
EfRepositoryBaseOfTEntityAndTPrimaryKey.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
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
241
242
243
244
245
246
247
248
249
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Abp.Collections.Extensions;
using Abp.Data;
using Abp.Domain.Entities;
using Abp.Domain.Repositories;
namespace Abp.EntityFramework.Repositories
{
/// <summary>
/// Implements IRepository for Entity Framework.
/// </summary>
/// <typeparam name="TDbContext">DbContext which contains <typeparamref name="TEntity"/>.</typeparam>
/// <typeparam name="TEntity">Type of the Entity for this repository</typeparam>
/// <typeparam name="TPrimaryKey">Primary key of the entity</typeparam>
public class EfRepositoryBase<TDbContext, TEntity, TPrimaryKey> : AbpRepositoryBase<TEntity, TPrimaryKey>, IRepositoryWithDbContext
where TEntity : class, IEntity<TPrimaryKey>
where TDbContext : DbContext
{
/// <summary>
/// Gets EF DbContext object.
/// </summary>
public virtual TDbContext Context => _dbContextProvider.GetDbContext(MultiTenancySide);
/// <summary>
/// Gets DbSet for given entity.
/// </summary>
public virtual DbSet<TEntity> Table => Context.Set<TEntity>();
public virtual DbTransaction Transaction
{
get
{
return (DbTransaction)TransactionProvider?.GetActiveTransaction(new ActiveTransactionProviderArgs
{
{"ContextType", typeof(TDbContext) },
{"MultiTenancySide", MultiTenancySide }
});
}
}
public virtual DbConnection Connection
{
get
{
var connection = Context.Database.Connection;
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
return connection;
}
}
private readonly IDbContextProvider<TDbContext> _dbContextProvider;
public IActiveTransactionProvider TransactionProvider { private get; set; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="dbContextProvider"></param>
public EfRepositoryBase(IDbContextProvider<TDbContext> dbContextProvider)
{
_dbContextProvider = dbContextProvider;
}
public override IQueryable<TEntity> GetAll()
{
return Table;
}
public override IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors)
{
if (propertySelectors.IsNullOrEmpty())
{
return GetAll();
}
var query = GetAll();
foreach (var propertySelector in propertySelectors)
{
query = query.Include(propertySelector);
}
return query;
}
public override async Task<List<TEntity>> GetAllListAsync()
{
return await GetAll().ToListAsync();
}
public override async Task<List<TEntity>> GetAllListAsync(Expression<Func<TEntity, bool>> predicate)
{
return await GetAll().Where(predicate).ToListAsync();
}
public override async Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> predicate)
{
return await GetAll().SingleAsync(predicate);
}
public override async Task<TEntity> FirstOrDefaultAsync(TPrimaryKey id)
{
return await GetAll().FirstOrDefaultAsync(CreateEqualityExpressionForId(id));
}
public override async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate)
{
return await GetAll().FirstOrDefaultAsync(predicate);
}
public override TEntity Insert(TEntity entity)
{
return Table.Add(entity);
}
public override Task<TEntity> InsertAsync(TEntity entity)
{
return Task.FromResult(Table.Add(entity));
}
public override TPrimaryKey InsertAndGetId(TEntity entity)
{
entity = Insert(entity);
if (entity.IsTransient())
{
Context.SaveChanges();
}
return entity.Id;
}
public override async Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity)
{
entity = await InsertAsync(entity);
if (entity.IsTransient())
{
await Context.SaveChangesAsync();
}
return entity.Id;
}
public override TPrimaryKey InsertOrUpdateAndGetId(TEntity entity)
{
entity = InsertOrUpdate(entity);
if (entity.IsTransient())
{
Context.SaveChanges();
}
return entity.Id;
}
public override async Task<TPrimaryKey> InsertOrUpdateAndGetIdAsync(TEntity entity)
{
entity = await InsertOrUpdateAsync(entity);
if (entity.IsTransient())
{
await Context.SaveChangesAsync();
}
return entity.Id;
}
public override TEntity Update(TEntity entity)
{
AttachIfNot(entity);
Context.Entry(entity).State = EntityState.Modified;
return entity;
}
public override Task<TEntity> UpdateAsync(TEntity entity)
{
AttachIfNot(entity);
Context.Entry(entity).State = EntityState.Modified;
return Task.FromResult(entity);
}
public override void Delete(TEntity entity)
{
AttachIfNot(entity);
Table.Remove(entity);
}
public override void Delete(TPrimaryKey id)
{
var entity = Table.Local.FirstOrDefault(ent => EqualityComparer<TPrimaryKey>.Default.Equals(ent.Id, id));
if (entity == null)
{
entity = FirstOrDefault(id);
if (entity == null)
{
return;
}
}
Delete(entity);
}
public override async Task<int> CountAsync()
{
return await GetAll().CountAsync();
}
public override async Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate)
{
return await GetAll().Where(predicate).CountAsync();
}
public override async Task<long> LongCountAsync()
{
return await GetAll().LongCountAsync();
}
public override async Task<long> LongCountAsync(Expression<Func<TEntity, bool>> predicate)
{
return await GetAll().Where(predicate).LongCountAsync();
}
protected virtual void AttachIfNot(TEntity entity)
{
if (!Table.Local.Contains(entity))
{
Table.Attach(entity);
}
}
public DbContext GetDbContext()
{
return Context;
}
}
}