Repository navigation
Expand file tree
/
Copy pathRepository.cs
More file actions
56 lines (48 loc) · 1.45 KB
/
Copy pathRepository.cs
File metadata and controls
56 lines (48 loc) · 1.45 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 NRepository.Abstractions;
using Microsoft.EntityFrameworkCore;
using NSpecifications;
using System;
using System.Collections.Generic;
using System.Linq;
namespace NRepository.EF
{
/// <summary>
/// Generic repository, will probably live in another class library project that can be shared with many other repos.
/// </summary>
/// <typeparam name="T"></typeparam>
public class Repository<T> : IRepository<T> where T: class
{
public Repository(DbContext context) //Do not create another constructor
{
Context = context;
}
protected DbContext Context { get; set; }
public void Add(T entity)
{
Context.Add(entity);
Context.SaveChanges();
}
public T GetSingle(ASpec<T> specification)
{
return Context.Set<T>().Where(specification).SingleOrDefault();
}
public List<T> Get(ASpec<T> specification)
{
return Context.Set<T>().Where(specification).ToList();
}
public List<T> Get()
{
return Context.Set<T>().ToList();
}
public void Save(T entity) //I'm thinking of removing this, not sure yet.
{
Context.SaveChanges();
}
public void Remove(Guid id)
{
var entity = Context.Set<T>().Find(id);
Context.Set<T>().Remove(entity);
Context.SaveChanges();
}
}
}