-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepository.cs
More file actions
70 lines (59 loc) · 1.73 KB
/
Repository.cs
File metadata and controls
70 lines (59 loc) · 1.73 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using AzureDevopsTracker.Data.Context;
using AzureDevopsTracker.Entities;
using AzureDevopsTracker.Interfaces.Internals;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AzureDevopsTracker.Data
{
internal abstract class Repository<TEntity> : IRepository<TEntity> where TEntity : Entity
{
protected readonly AzureDevopsTrackerContext Db;
protected readonly DbSet<TEntity> DbSet;
public Repository(AzureDevopsTrackerContext db)
{
Db = db;
DbSet = db.Set<TEntity>();
}
public virtual async Task Add(TEntity entity)
{
await DbSet.AddAsync(entity);
}
public virtual async Task Add(IEnumerable<TEntity> entities)
{
await DbSet.AddRangeAsync(entities);
}
public virtual void Update(TEntity entity)
{
DbSet.Update(entity);
}
public virtual void Update(IEnumerable<TEntity> entities)
{
DbSet.UpdateRange(entities);
}
public virtual void Delete(TEntity entity)
{
DbSet.Remove(entity);
}
public virtual async Task<TEntity> GetById(string id)
{
return await DbSet
.FirstOrDefaultAsync(x => x.Id == id);
}
public async Task<bool> Exist(string id)
{
return await DbSet
.AnyAsync(x => x.Id == id);
}
public async Task SaveChangesAsync()
{
await Db.SaveChangesAsync();
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
}
}