Implement generic repo pattern and unitofwork
This commit is contained in:
77
GerbilManagerWebAPI/DAL/GenericRepository.cs
Normal file
77
GerbilManagerWebAPI/DAL/GenericRepository.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System.Linq.Expressions;
|
||||
using GerbilManagerWebAPI.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GerbilManagerWebAPI.DAL
|
||||
{
|
||||
public class GenericRepository<TEntity> where TEntity : class
|
||||
{
|
||||
internal ApplicationContext context;
|
||||
internal DbSet<TEntity> dbSet;
|
||||
|
||||
public GenericRepository(ApplicationContext context)
|
||||
{
|
||||
this.context = context;
|
||||
this.dbSet = context.Set<TEntity>();
|
||||
}
|
||||
|
||||
public virtual IEnumerable<TEntity> Get(
|
||||
Expression<Func<TEntity, bool>> filter = null,
|
||||
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
|
||||
string includeProperties = "")
|
||||
{
|
||||
IQueryable<TEntity> query = dbSet;
|
||||
|
||||
if (filter != null)
|
||||
{
|
||||
query = query.Where(filter);
|
||||
}
|
||||
|
||||
foreach (var includeProperty in includeProperties.Split
|
||||
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
query = query.Include(includeProperty);
|
||||
}
|
||||
|
||||
if (orderBy != null)
|
||||
{
|
||||
return orderBy(query).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
return query.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual TEntity GetByID(object id)
|
||||
{
|
||||
return dbSet.Find(id);
|
||||
}
|
||||
|
||||
public virtual void Insert(TEntity entity)
|
||||
{
|
||||
dbSet.Add(entity);
|
||||
}
|
||||
|
||||
public virtual void Delete(object id)
|
||||
{
|
||||
TEntity entityToDelete = dbSet.Find(id);
|
||||
Delete(entityToDelete);
|
||||
}
|
||||
|
||||
public virtual void Delete(TEntity entityToDelete)
|
||||
{
|
||||
if (context.Entry(entityToDelete).State == EntityState.Detached)
|
||||
{
|
||||
dbSet.Attach(entityToDelete);
|
||||
}
|
||||
dbSet.Remove(entityToDelete);
|
||||
}
|
||||
|
||||
public virtual void Update(TEntity entityToUpdate)
|
||||
{
|
||||
dbSet.Attach(entityToUpdate);
|
||||
context.Entry(entityToUpdate).State = EntityState.Modified;
|
||||
}
|
||||
}
|
||||
}
|
||||
79
GerbilManagerWebAPI/DAL/UnitOfWork.cs
Normal file
79
GerbilManagerWebAPI/DAL/UnitOfWork.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using GerbilManagerWebAPI.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GerbilManagerWebAPI.DAL
|
||||
{
|
||||
public class UnitOfWork : IDisposable
|
||||
{
|
||||
private ApplicationContext context;
|
||||
private GenericRepository<Gerbil> gerbilRepository;
|
||||
private GenericRepository<Litter> litterRepository;
|
||||
private GenericRepository<Breeder> breederRepository;
|
||||
|
||||
public GenericRepository<Gerbil> GerbilRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.gerbilRepository == null)
|
||||
{
|
||||
this.gerbilRepository = new GenericRepository<Gerbil>(context);
|
||||
}
|
||||
return gerbilRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public GenericRepository<Litter> LitterRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.litterRepository == null)
|
||||
{
|
||||
this.litterRepository = new GenericRepository<Litter>(context);
|
||||
}
|
||||
return litterRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public GenericRepository<Breeder> BreederRepository
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.breederRepository == null)
|
||||
{
|
||||
this.breederRepository = new GenericRepository<Breeder>(context);
|
||||
}
|
||||
return breederRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public UnitOfWork(ApplicationContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
private bool disposed = false;
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!this.disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
context.Dispose();
|
||||
}
|
||||
}
|
||||
this.disposed = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user