| | 1 | | using Domain.Core.Aggreggates; |
| | 2 | | using System.Linq.Expressions; |
| | 3 | |
|
| | 4 | | namespace Repository.Persistency.Generic; |
| | 5 | | public class GenericRepositorio<T> : IRepositorio<T> where T : BaseDomain, new() |
| | 6 | | { |
| | 7 | | protected readonly RegisterContext _context; |
| | 8 | |
|
| 9 | 9 | | public GenericRepositorio(RegisterContext context) |
| 9 | 10 | | { |
| 9 | 11 | | this._context = context; |
| 9 | 12 | | } |
| | 13 | |
|
| | 14 | | public virtual void Insert(T item) |
| 2 | 15 | | { |
| 2 | 16 | | this._context.Add(item); |
| 2 | 17 | | this._context.SaveChanges(); |
| 2 | 18 | | } |
| | 19 | |
|
| | 20 | | public virtual List<T> GetAll() |
| 1 | 21 | | { |
| 1 | 22 | | return this._context.Set<T>().ToList(); |
| 1 | 23 | | } |
| | 24 | |
|
| | 25 | | public virtual T Get(Guid id) |
| 1 | 26 | | { |
| 1 | 27 | | return this._context.Set<T>().SingleOrDefault(prop => prop.Id.Equals(id)); |
| 1 | 28 | | } |
| | 29 | |
|
| | 30 | | public virtual void Update(T entity) |
| 2 | 31 | | { |
| 2 | 32 | | var existingEntity = _context.Set<T>().Find(entity.Id); |
| 2 | 33 | | this._context.Set<T>().Entry(existingEntity).CurrentValues.SetValues(entity); |
| 1 | 34 | | this._context.SaveChanges(); |
| 1 | 35 | | } |
| | 36 | |
|
| | 37 | | public virtual void Delete(T entity) |
| 2 | 38 | | { |
| 2 | 39 | | this._context.Set<T>().Remove(entity); |
| 1 | 40 | | this._context.SaveChanges(); |
| 1 | 41 | | } |
| | 42 | |
|
| | 43 | | public virtual bool Exists(Guid id) |
| 0 | 44 | | { |
| 0 | 45 | | return this._context.Set<T>().Any(prop => prop.Id.Equals(id)); |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | public IEnumerable<T>? Find(Expression<Func<T, bool>> expression) |
| 0 | 49 | | { |
| 0 | 50 | | return this._context.Set<T>().Where(expression); |
| 0 | 51 | | } |
| | 52 | | } |