| | 1 | | using Domain.Core.Aggreggates; |
| | 2 | | using Microsoft.EntityFrameworkCore; |
| | 3 | | using System.Linq.Expressions; |
| | 4 | |
|
| | 5 | | namespace Repository.Abastractions; |
| | 6 | |
|
| | 7 | | public abstract class BaseRepository<T> where T : BaseDomain, new() |
| | 8 | | { |
| 51 | 9 | | private DbContext Context { get; set; } |
| | 10 | |
|
| 40 | 11 | | protected BaseRepository(DbContext context) |
| 40 | 12 | | { |
| 40 | 13 | | Context = context; |
| 40 | 14 | | } |
| | 15 | |
|
| | 16 | | public virtual void Insert(T entity) |
| 1 | 17 | | { |
| 1 | 18 | | Context.Add(entity); |
| 1 | 19 | | Context.SaveChanges(); |
| 1 | 20 | | } |
| | 21 | |
|
| | 22 | | public virtual void Update(T entity) |
| 1 | 23 | | { |
| 1 | 24 | | Context.Update(entity); |
| 1 | 25 | | Context.SaveChanges(); |
| 1 | 26 | | } |
| | 27 | |
|
| | 28 | | public virtual void Delete(T entity) |
| 1 | 29 | | { |
| 1 | 30 | | Context.Remove(entity); |
| 1 | 31 | | Context.SaveChanges(); |
| 1 | 32 | | } |
| | 33 | |
|
| | 34 | | public virtual IEnumerable<T> GetAll() |
| 1 | 35 | | { |
| 1 | 36 | | return Context.Set<T>().ToList(); |
| 1 | 37 | | } |
| | 38 | |
|
| | 39 | | public virtual T Get(Guid id) |
| 2 | 40 | | { |
| 2 | 41 | | return Context.Set<T>().Find(id); |
| 2 | 42 | | } |
| | 43 | |
|
| | 44 | | public virtual IEnumerable<T> Find(Expression<Func<T, bool>> expression) |
| 2 | 45 | | { |
| 2 | 46 | | return Context.Set<T>().Where(expression); |
| 2 | 47 | | } |
| | 48 | |
|
| | 49 | | public virtual bool Exists(Guid id) |
| 1 | 50 | | { |
| 1 | 51 | | return this.Get(id) != null; |
| 1 | 52 | | } |
| | 53 | |
|
| | 54 | | public virtual bool Exists(Expression<Func<T, bool>> expression) |
| 1 | 55 | | { |
| 1 | 56 | | return Find(expression).Any(); |
| 1 | 57 | | } |
| | 58 | | } |