-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEFRepository.cs
127 lines (105 loc) · 3.34 KB
/
EFRepository.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
using MFramework.Services.DataAccess.Abstract;
using MFramework.Services.DataAccess.UnitOfWork;
using MFramework.Services.Entities.Abstract;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace MFramework.Services.DataAccess.EntityFramework
{
public abstract class EFRepository<TEntity, TKey, TContext> : IRepository<TEntity, TKey>
where TEntity : EntityBase<TKey>
where TContext : DbContext
{
protected readonly TContext Context;
protected readonly DbSet<TEntity> Table;
public EFRepository(TContext context)
{
Context = context ?? throw new ArgumentNullException(nameof(context));
Table = Context.Set<TEntity>();
}
public virtual TEntity Add(TEntity entity)
{
return Table.Add(entity);
}
public virtual async Task<TEntity> AddAsync(TEntity entity)
{
return await Task.FromResult(Add(entity));
}
public virtual TEntity Find(TKey id)
{
return Table.Find(id);
}
public virtual async Task<TEntity> FindAsync(TKey id)
{
return await Table.FindAsync(id);
}
public virtual TEntity Find(params object[] ids)
{
return Table.Find(ids);
}
public virtual async Task<TEntity> FindAsync(params object[] ids)
{
return await Table.FindAsync(ids);
}
public virtual async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate)
{
return await Table.FirstOrDefaultAsync(predicate);
}
public virtual async Task<IList<TEntity>> ListAsync()
{
return await Table.ToListAsync();
}
public virtual void Remove(TKey id)
{
var entity = Table.Find(id);
if (entity == null) return;
Table.Remove(entity);
}
public virtual async Task RemoveAsync(TKey id)
{
var entity = await Table.FindAsync(id);
if (entity == null) return;
await Task.Run(() => Table.Remove(entity));
}
public virtual void Remove(TEntity entity)
{
AttachIfNot(entity);
Table.Remove(entity);
}
public virtual async Task RemoveAsync(TEntity entity)
{
await Task.Run(() => Remove(entity));
}
public virtual void Update(TKey id, TEntity entity)
{
AttachIfNot(entity);
Context.Entry(entity).State = EntityState.Modified;
}
public virtual async Task UpdateAsync(TKey id, TEntity entity)
{
await Task.Run(() => Update(id, entity));
}
protected virtual void AttachIfNot(TEntity entity)
{
if (!Table.Local.Contains(entity))
{
Table.Attach(entity);
}
}
public virtual IQueryable<TEntity> Queryable()
{
return Table.AsQueryable();
}
public virtual int Save()
{
return Context.SaveChanges();
}
public virtual async Task<int> SaveAsync()
{
return await Context.SaveChangesAsync();
}
}
}