-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathEntityFrameworkRepository.cs
103 lines (86 loc) · 3.7 KB
/
EntityFrameworkRepository.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
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace Liquid.Repository.EntityFramework
{
/// <summary>
/// Implements the EntityFramework repository.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <typeparam name="TIdentifier">The type of the identifier.</typeparam>
/// <typeparam name="TContext">The type of the <see cref="DbContext"/>.</typeparam>
/// <seealso cref="ILiquidRepository{TEntity, TIdentifier}" />
#pragma warning disable S2436 // Types and methods should not have too many generic parameters
public class EntityFrameworkRepository<TEntity, TIdentifier, TContext> : ILiquidRepository<TEntity, TIdentifier> where TEntity : LiquidEntity<TIdentifier>, new() where TContext : DbContext
#pragma warning restore S2436 // Types and methods should not have too many generic parameters
{
///<inheritdoc/>
public IEntityFrameworkDataContext<TContext> EntityDataContext { get; }
///<inheritdoc/>
public ILiquidDataContext DataContext => EntityDataContext;
private readonly TContext _dbClient;
private readonly DbSet<TEntity> _dbSet;
private readonly IQueryable<TEntity> _queryableReadOnly;
/// <summary>
/// Initializes a new instance of the <see cref="EntityFrameworkRepository{TEntity, TIdentifier, TContext}" /> class.
/// </summary>
/// <param name="dataContext">The data context.</param>
/// <exception cref="ArgumentNullException">
/// telemetryFactory
/// or
/// dataContext
/// </exception>
public EntityFrameworkRepository(IEntityFrameworkDataContext<TContext> dataContext)
{
EntityDataContext = dataContext ?? throw new ArgumentNullException(nameof(dataContext));
_dbClient = dataContext.DbClient;
_dbSet = _dbClient.Set<TEntity>();
_queryableReadOnly = _dbSet.AsNoTracking();
}
///<inheritdoc/>
public async Task AddAsync(TEntity entity)
{
await _dbSet.AddAsync(entity);
await _dbClient.SaveChangesAsync();
}
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
///<inheritdoc/>
public async Task<IEnumerable<TEntity>> FindAllAsync()
{
IEnumerable<TEntity> returnValue = _queryableReadOnly;
return returnValue;
}
///<inheritdoc/>
public async Task<TEntity> FindByIdAsync(TIdentifier id)
{
var returnValue = _queryableReadOnly.FirstOrDefault(o => o.Id.Equals(id));
return returnValue;
}
///<inheritdoc/>
public async Task RemoveByIdAsync(TIdentifier id)
{
var obj = await _dbSet.FirstOrDefaultAsync(o => o.Id.Equals(id));
if (obj == null) return;
_dbSet.Remove(obj);
await _dbClient.SaveChangesAsync();
}
///<inheritdoc/>
public async Task UpdateAsync(TEntity entity)
{
_dbClient.Detach<TEntity>(o => o.Id.Equals(entity.Id));
_dbClient.Update(entity);
await _dbClient.SaveChangesAsync();
}
///<inheritdoc/>
public async Task<IEnumerable<TEntity>> WhereAsync(Expression<Func<TEntity, bool>> whereClause)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
var result = _queryableReadOnly.Where(whereClause);
var returnValue = result.AsEnumerable();
return returnValue;
}
}
}