[英]Unable to use proper Generic Repository Structure C# MVC With Entity Framework
我有一个名为IRepository的接口和一个名为Repository的类,以在C#MVC中实现Repository模式
public interface IRepository<TEntity>
{
void Insert(TEntity entity);
void Delete(TEntity entity);
}
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
DbSet<TEntity> DbSet;
TestDBEntities dataContext;
public Repository(TestDBEntities dataContext)
{
this.dataContext = dataContext;
this.DbSet = dataContext.Set<TEntity>();
}
public void Insert(TEntity entity)
{
DbSet.Add(entity);
dataContext.SaveChanges();
}
public void Delete(TEntity entity)
{
DbSet.Remove(entity);
}
public IQueryable<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate)
{
return DbSet.Where(predicate);
}
}
这是我的控制器:
public class HomeController : Controller
{
private IRepository<Tbl_EmpDetails> _EmpDetails;
public HomeController()
{
_EmpDetails = new Repository<Tbl_EmpDetails>(new TestDBEntities());
}
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Tbl_EmpDetails empDetials)
{
_EmpDetails.Insert(empDetials);
return Redirect("/");
}
}
按照我在控制器中的想法,我不必使用“ new TestDBEntities()”,因为当Insert,Update,Delete或控制器的任何其他函数将被调用且将不使用存储库时,将一次又一次创建EDMX实例。 。 (TestDBEntity是我的EDMX的对象)。
_EmpDetails = new Repository<Tbl_EmpDetails>(new TestDBEntities());
Tbl_EmpDetails是EDMX下的表。
请建议我根据我的代码实现存储库结构的最佳方法是什么,如果您还有其他建议可以改进我的代码,请给我建议。
提前致谢。
这就是我组织项目的方式。
Project.Web
-> Controllers
Project.Domain
-> Contracts
-> Services
Project.Data
-> Contracts
-> Repositories
因此,在我的控制器中,仅公开了服务(因此,所有实现都被封装了)。 我使用ninject
处理这种依赖关系。
public SomeController(IService service)
{
}
在我的域名项目中
实现了一个接口和派生该接口的类。 因此,基本上,这仅调用存储库项目,其他业务逻辑应驻留在此处。
介面
public interface IService
{
IEnumerable<SomeClass> FindAll();
}
实施
public class Service : IService
{
private ISomeRepository _someRepository; // Exposed instance of my repo
public IEnumerable<SomeClass> FindAll()
{
return _someRepository.FindAll();
}
}
在我的数据项目中
我也有两种实现方式,一种用于接口,另一种用于实现该接口的类。
介面
public interface ISomeRepository
{
IEnumerable<Some> FindAll();
}
实施
public class SomeRepository : BaseRepository, ISomeRepository
{
public R(IUnitOfWork unitOfWork)
: base(unitOfWork)
{
}
public IEnumerable<SomeClass> FindAll()
{
return this.GetDbSet<SomeClass>();
}
}
如果您想知道BaseRepository
和UnitOfWork
的用途是什么,它将保存我们模型(EDMX)的上下文。
基本资料库
public class BaseRepository
{
protected IUnitOfWork UnitOfWork { get; set; }
protected MyEntities Context
{
get { return (MyEntities)this.UnitOfWork.Database; }
}
public BaseRepository(IUnitOfWork unitOfWork)
{
if (unitOfWork == null) throw new ArgumentNullException("unitOfWork");
this.UnitOfWork = unitOfWork;
}
protected virtual DbSet<TEntity> GetDbSet<TEntity>() where TEntity : class
{
return this.Context.Set<TEntity>();
}
protected virtual void SetEntityState(object entity, EntityState entityState)
{
this.Context.Entry(entity).State = entityState;
}
}
工作单位
public class UnitOfWork : IUnitOfWork, IDisposable
{
public DbContext Database { get; private set; }
public UnitOfWork(DbContext dbContext)
{
Database = dbContext;
}
public void SaveChanges()
{
Database.SaveChanges();
}
public void Dispose()
{
Database.Dispose();
}
}
IUnitOfWork接口
public interface IUnitOfWork
{
DbContext Database { get; }
/// <summary>
/// Saves changes to all objects that have changed within the unit of work.
/// </summary>
void SaveChanges();
}
因此,为此,我们迎合了关注点分离的问题,每个项目都达到了其真正目的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.