-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadRepository.cs
More file actions
56 lines (52 loc) · 1.85 KB
/
ReadRepository.cs
File metadata and controls
56 lines (52 loc) · 1.85 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using ECommerceAPI.Application.Repositories;
using ECommerceAPI.Domain.Entities.Common;
using ECommerceAPI.Persistence.Contexts;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ECommerceAPI.Persistence.Repositories
{
public class ReadRepository<T> : IReadRepository<T> where T : BaseEntity
{
private readonly ECommerceAPIDbContext _context;
public ReadRepository(ECommerceAPIDbContext context)
{
_context = context;
}
public DbSet<T> Table => _context.Set<T>();
public IQueryable<T> GetAll(bool tracking = true)
{
var query = Table.AsQueryable();
if (!tracking)
query = query.AsNoTracking();
return query;
}
public IQueryable<T> GetWhere(Expression<Func<T, bool>> method, bool tracking = true)
{
var query = Table.Where(method);
if (!tracking)
query = query.AsNoTracking();
return query;
}
public async Task<T> GetSingleAsync(Expression<Func<T, bool>> method, bool tracking = true)
{
var query = Table.AsQueryable();
if (!tracking)
query = Table.AsNoTracking();
return await query.FirstOrDefaultAsync(method);
}
public async Task<T> GetByIdAsync(string id, bool tracking = true)
//=> await Table.FirstOrDefaultAsync(data => data.Id == Guid.Parse(id));
//=> await Table.FindAsync(Guid.Parse(id));
{
var query = Table.AsQueryable();
if (!tracking)
query = Table.AsNoTracking();
return await query.FirstOrDefaultAsync(data => data.Id == Guid.Parse(id));
}
}
}