-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepository.cs
More file actions
40 lines (31 loc) · 1.28 KB
/
Repository.cs
File metadata and controls
40 lines (31 loc) · 1.28 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
using Application;
using Domain;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
namespace Infrastructure
{
public class Repository<TEntity> : IRepository<TEntity>
where TEntity : EntityBase
{
protected readonly IdentityContext _context;
public Repository(IdentityContext context)
{
_context = context;
}
public virtual IQueryable<TEntity> Init()
=> _context.Set<TEntity>();
public virtual async Task BasicAddAsync(TEntity entity, CancellationToken cancellationToken = default)
=> await _context.Set<TEntity>().AddAsync(entity, cancellationToken);
public virtual void BasicRemove(TEntity entity)
=> _context.Set<TEntity>().Remove(entity);
public virtual async Task<bool> CheckIfExistAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default)
=> await Init().AnyAsync(expression, cancellationToken);
public virtual IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression, bool autoTrack = true)
{
IQueryable<TEntity> query = Init();
if (!autoTrack)
query.AsNoTracking();
return query.Where(expression);
}
}
}