Working full stack forums get
This commit is contained in:
@@ -52,11 +52,15 @@
|
||||
<Compile Include="FoorumDbContext.cs" />
|
||||
<Compile Include="Helpers\DbInitializer.cs" />
|
||||
<Compile Include="Interfaces\IFoorumDbContext.cs" />
|
||||
<Compile Include="Interfaces\IFoorumRepository.cs" />
|
||||
<Compile Include="Interfaces\IPostRepository.cs" />
|
||||
<Compile Include="Interfaces\IRepository.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Repositories\FoorumRepository.cs" />
|
||||
<Compile Include="Repositories\PostRepository.cs" />
|
||||
<Compile Include="Repositories\RepositoryBase.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Repositories\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
<None Include="packages.config" />
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace DAL.Interfaces
|
||||
{
|
||||
interface IFoorumDbContext
|
||||
public interface IFoorumDbContext
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
6
DAL/Interfaces/IFoorumRepository.cs
Normal file
6
DAL/Interfaces/IFoorumRepository.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
using DAL.Domain;
|
||||
|
||||
namespace DAL.Interfaces
|
||||
{
|
||||
public interface IFoorumRepository: IRepository<Foorum> { }
|
||||
}
|
||||
6
DAL/Interfaces/IPostRepository.cs
Normal file
6
DAL/Interfaces/IPostRepository.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
using DAL.Domain;
|
||||
|
||||
namespace DAL.Interfaces
|
||||
{
|
||||
public interface IPostRepository: IRepository<Post> { }
|
||||
}
|
||||
20
DAL/Interfaces/IRepository.cs
Normal file
20
DAL/Interfaces/IRepository.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
|
||||
namespace DAL.Interfaces
|
||||
{
|
||||
public interface IRepository<T> where T : class
|
||||
{
|
||||
T Add(T entity);
|
||||
T Update(T entity);
|
||||
void Delete(T entity);
|
||||
void Delete(Expression<Func<T, bool>> where);
|
||||
T GetById(int id);
|
||||
T Get(Expression<Func<T, bool>> where);
|
||||
IEnumerable<T> GetAll();
|
||||
IEnumerable<T> GetMany(Expression<Func<T, bool>> where);
|
||||
int SaveChanges();
|
||||
}
|
||||
}
|
||||
15
DAL/Repositories/FoorumRepository.cs
Normal file
15
DAL/Repositories/FoorumRepository.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using DAL.Domain;
|
||||
using DAL.Interfaces;
|
||||
|
||||
namespace DAL.Repositories
|
||||
{
|
||||
public class FoorumRepository: RepositoryBase<Foorum>, IFoorumRepository
|
||||
{
|
||||
public FoorumRepository(IFoorumDbContext ctx): base(ctx) { }
|
||||
}
|
||||
}
|
||||
15
DAL/Repositories/PostRepository.cs
Normal file
15
DAL/Repositories/PostRepository.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using DAL.Domain;
|
||||
using DAL.Interfaces;
|
||||
|
||||
namespace DAL.Repositories
|
||||
{
|
||||
public class PostRepository: RepositoryBase<Post>, IPostRepository
|
||||
{
|
||||
public PostRepository(IFoorumDbContext ctx) : base(ctx) { }
|
||||
}
|
||||
}
|
||||
80
DAL/Repositories/RepositoryBase.cs
Normal file
80
DAL/Repositories/RepositoryBase.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Entity;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using DAL.Interfaces;
|
||||
|
||||
namespace DAL.Repositories
|
||||
{
|
||||
public class RepositoryBase<T> where T : class
|
||||
{
|
||||
protected DbContext RepositoryDataContext;
|
||||
protected DbSet<T> RepositoryDbSet;
|
||||
|
||||
|
||||
public RepositoryBase(IFoorumDbContext dbContext)
|
||||
{
|
||||
RepositoryDataContext = dbContext as DbContext;
|
||||
if (RepositoryDataContext == null)
|
||||
{
|
||||
throw new ArgumentNullException(paramName: nameof(dbContext));
|
||||
}
|
||||
RepositoryDbSet = RepositoryDataContext.Set<T>();
|
||||
if (RepositoryDbSet == null)
|
||||
{
|
||||
throw new NullReferenceException(message: nameof(RepositoryDbSet));
|
||||
}
|
||||
}
|
||||
|
||||
public T Add(T entity)
|
||||
{
|
||||
var ret = RepositoryDbSet.Add(entity);
|
||||
SaveChanges();
|
||||
return ret;
|
||||
}
|
||||
|
||||
public T Update(T entity)
|
||||
{
|
||||
RepositoryDbSet.Attach(entity);
|
||||
RepositoryDataContext.Entry(entity).State = EntityState.Modified;
|
||||
return entity;
|
||||
}
|
||||
|
||||
public void Delete(T entity)
|
||||
{
|
||||
RepositoryDbSet.Remove(entity);
|
||||
}
|
||||
|
||||
public void Delete(Expression<Func<T, bool>> where)
|
||||
{
|
||||
IEnumerable<T> objects = RepositoryDbSet.Where<T>(where).AsEnumerable();
|
||||
foreach (T obj in objects)
|
||||
RepositoryDbSet.Remove(obj);
|
||||
}
|
||||
|
||||
public T GetById(int id)
|
||||
{
|
||||
return RepositoryDbSet.Find(id);
|
||||
}
|
||||
|
||||
public virtual IEnumerable<T> GetAll()
|
||||
{
|
||||
return RepositoryDbSet.ToList();
|
||||
}
|
||||
|
||||
public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
|
||||
{
|
||||
return RepositoryDbSet.Where(where).ToList();
|
||||
}
|
||||
|
||||
public T Get(Expression<Func<T, bool>> where)
|
||||
{
|
||||
return RepositoryDbSet.Where(where).FirstOrDefault<T>();
|
||||
}
|
||||
public int SaveChanges()
|
||||
{
|
||||
return RepositoryDataContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user