Interfaces
This commit is contained in:
74
DAL/Repositories/EFRepository.cs
Normal file
74
DAL/Repositories/EFRepository.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
14
DAL/Repositories/PersonRepository.cs
Normal file
14
DAL/Repositories/PersonRepository.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System.Data.Entity;
|
||||
using Domain;
|
||||
using Interfaces;
|
||||
|
||||
namespace DAL.Repositories
|
||||
{
|
||||
public class PersonRepository : EFRepository<Person>, IPersonRepository
|
||||
{
|
||||
public PersonRepository(DbContext dbContext) : base(dbContext)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user