75 lines
1.8 KiB
C#
75 lines
1.8 KiB
C#
using Interfaces;
|
|
using System;
|
|
using System.CodeDom;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DAL.Repositories
|
|
{
|
|
public class EFRepository<TEntity> : IRespository<TEntity>
|
|
where TEntity : class
|
|
{
|
|
protected DbContext RepositoryDbContext;
|
|
protected DbSet<TEntity> RepositoryDbSet;
|
|
|
|
public EFRepository(DbContext dbContext)
|
|
{
|
|
if (dbContext == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(dbContext));
|
|
}
|
|
RepositoryDbContext = dbContext;
|
|
RepositoryDbSet = dbContext.Set<TEntity>();
|
|
if (RepositoryDbSet == null)
|
|
{
|
|
throw new NullReferenceException(nameof(RepositoryDbSet));
|
|
}
|
|
}
|
|
|
|
public List<TEntity> All
|
|
{
|
|
get
|
|
{
|
|
var resCount = RepositoryDbSet.Count();
|
|
if (resCount < 10)
|
|
{
|
|
return RepositoryDbSet.ToList();
|
|
}
|
|
throw new Exception("To many records in result set. Custom data access method needed");
|
|
}
|
|
}
|
|
public TEntity Find(int id)
|
|
{
|
|
RepositoryDbSet.Find(id);
|
|
}
|
|
|
|
public void Remove(int id)
|
|
{
|
|
Remove(Find(id));
|
|
}
|
|
|
|
public void Remove(TEntity entity)
|
|
{
|
|
RepositoryDbSet.Remove(entity);
|
|
}
|
|
|
|
public TEntity Add(TEntity entity)
|
|
{
|
|
return RepositoryDbSet.Add(entity);
|
|
}
|
|
|
|
public void Update(TEntity entity)
|
|
{
|
|
RepositoryDbSet.Attach(entity);
|
|
}
|
|
|
|
public int SaveChanges()
|
|
{
|
|
return RepositoryDbContext.SaveChanges();
|
|
}
|
|
}
|
|
}
|