-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Linq.Expressions; | ||
|
||
namespace AutomatedTestingApp.Repositories; | ||
|
||
public interface IRepository<TEntity> | ||
{ | ||
IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>>? filter = null, | ||
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null); | ||
|
||
TEntity GetById(object id); | ||
|
||
void Insert(TEntity entity); | ||
|
||
void Update(TEntity entity); | ||
|
||
void Delete(object id); | ||
|
||
void Delete(TEntity entityToDelete); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace AutomatedTestingApp.Repositories; | ||
|
||
public interface IUnitOfWork | ||
{ | ||
void Commit(); | ||
void Rollback(); | ||
IRepository<TEntity> GetRepository<TEntity>() where TEntity : class; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System.Linq.Expressions; | ||
using AutomatedTestingApp.Helpers; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace AutomatedTestingApp.Repositories; | ||
|
||
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class | ||
{ | ||
private readonly DbSet<TEntity> _dbSet; | ||
|
||
public Repository(DataContext context) | ||
{ | ||
_dbSet = context.Set<TEntity>(); | ||
} | ||
|
||
public IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>>? filter = null, | ||
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null) | ||
{ | ||
IQueryable<TEntity> query = _dbSet; | ||
|
||
if (filter != null) | ||
query = query.Where(filter); | ||
|
||
if (orderBy != null) | ||
return orderBy(query).ToList(); | ||
|
||
return _dbSet.ToList(); | ||
} | ||
|
||
public TEntity GetById(object id) | ||
{ | ||
return _dbSet.Find(id); | ||
Check warning on line 32 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs GitHub Actions / Build
Check warning on line 32 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs GitHub Actions / Build
Check warning on line 32 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs GitHub Actions / Build
|
||
} | ||
|
||
public void Insert(TEntity entity) | ||
{ | ||
_dbSet.Add(entity); | ||
} | ||
|
||
public void Update(TEntity entity) | ||
{ | ||
_dbSet.Attach(entity); | ||
} | ||
|
||
public void Delete(object id) | ||
{ | ||
TEntity entityToDelete = _dbSet.Find(id); | ||
Check warning on line 47 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs GitHub Actions / Build
Check warning on line 47 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs GitHub Actions / Build
Check warning on line 47 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs GitHub Actions / Build
|
||
|
||
if (entityToDelete == null) | ||
return; | ||
|
||
Delete(entityToDelete); | ||
} | ||
|
||
public void Delete(TEntity entityToDelete) | ||
{ | ||
if (_dbSet.Local.Contains(entityToDelete)) | ||
_dbSet.Remove(entityToDelete); | ||
else | ||
_dbSet.Attach(entityToDelete); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using AutomatedTestingApp.Helpers; | ||
|
||
namespace AutomatedTestingApp.Repositories; | ||
|
||
public class UnitOfWork : IUnitOfWork, IDisposable | ||
{ | ||
private readonly DataContext _context; | ||
private Dictionary<Type, Object> _repositories; | ||
|
||
public UnitOfWork(DataContext context) | ||
{ | ||
_context = context; | ||
_repositories = new Dictionary<Type, object>(); | ||
} | ||
|
||
public void Commit() | ||
{ | ||
_context.SaveChanges(); | ||
} | ||
|
||
public void Rollback() | ||
{ | ||
// TODO rollback changes here | ||
} | ||
|
||
public IRepository<TEntity> GetRepository<TEntity>() where TEntity : class | ||
{ | ||
if (_repositories.Keys.Contains(typeof(TEntity))) | ||
return (IRepository<TEntity>)_repositories[typeof(TEntity)]; | ||
var repository = new Repository<TEntity>(_context); | ||
_repositories.Add(typeof(TEntity), repository); | ||
return repository; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_context.Dispose(); | ||
} | ||
} |