您的位置:首页 > 其它

分享基于Entity Framework的Repository模式设计(附源码)

2013-09-07 23:53 330 查看

分享基于Entity Framework的Repository模式设计(附源码)

关于Repository模式,在这篇文章中有介绍,Entity Framework返回IEnumerable还是IQueryable?

这篇文章介绍的是使用Entity Framework实现的Repositoy模式设计,欢迎各位拍砖.


阅读目录:

一、实现的思路和结构图

二、Repository设计具体的实现代码

三、Repository设计的具体的使用

四、总结


一,实现的思路和结构图

总结一下,Repository在实际使用中,有下面三种特点:

Repository的共同性

有一些公共的方法(增删改查), 这些方法无关于Repository操作的是哪个实体类,可以把这些方法定义成接口IRepository<TEntity>, 然后有个基类BaseRepository<TEntity>实现该接口的方法。

常见的方法,比如Find, Filter, Delete, Create等

Repository的差异性

每个Repository类又会有一些差异性,应当允许它们能够继承BaseRepository<TEntity>之外,还能够再扩展自己的一些方法。所以每个类都可以再定义一个自己特有的接口,定义一些属于自己Repository的方法。

Repository的协同性

不同的Repository可能需要协同,Repository对数据的修改,需要在统一的保存.

最终实现的类结构图如下:

View Code
IUnitOfWorkRepository接口定义了方法获取特定的Repository, 执行存储过程, SaveChange方法提交修改,统一更新数据。

IUnitOfWorkRepository接口代码:

public interface IUnitOfWorkRepository : IDisposable
{
TRepository GetRepository<TRepository>() where TRepository : class;
void ExecuteProcedure(string procedureCommand, params object[] sqlParams);
void SaveChanges();
}


UnitOfWorkRepository代码, 代码中使用到了Autofac中的IComponentContext来获取Repository实例

public class UnitOfWorkRepository : IUnitOfWorkRepository
{
private readonly IComponentContext _componentContext;
protected readonly DbContext Context;

public UnitOfWorkRepository(DbContext context, IComponentContext componentContext)
{
Context = context;
_componentContext = componentContext;
}

public TRepository GetRepository<TRepository>() where TRepository : class
{
return _componentContext.Resolve<TRepository>();
}

public void ExecuteProcedure(string procedureCommand, params object[] sqlParams)
{
Context.Database.ExecuteSqlCommand(procedureCommand, sqlParams);
}

public void SaveChanges()
{
Context.SaveChanges();
}

public void Dispose()
{
if (Context != null)
Context.Dispose();
}
}


三, Repository设计的具体的使用

这里我们定义一个操作Student的Repository类,看看如何实际用于开发中。这里加入StudentRepository有自己特定的方法,需要获取所有的Students,这个扩展的方法名字叫GetAllStudents

那么定义一个接口IStudentRepository, 包含了方法GetAllStudents(), 同时继承IRepository<Student>接口

public interface IStudentRepository : IRepository<Student>
{
IEnumerable<dynamic> GetAllStudents();
}


然后定义StudentRepository类来实现这个接口

public class StudentRepository : BaseRepository<Student>, IStudentRepository
{
private readonly SchoolContext _context;

public StudentRepository(SchoolContext context)
: base(context)
{
_context = context;
}

public IEnumerable<dynamic> GetAllStudents()
{
return _context.Students;
}
}


使用Repository的代码如下:

IUnitOfWorkRepository unitOfWorkRepository = new UnitOfWorkRepository();

var studentRepository = unitOfWorkRepository.GetRepository<IStudentRepository>();
var students = studentRepository.GetAllStudents();

//同时你也可以使用定义于IRepository<Student>中的方法, 比如
//unitOfWorkRepository.Delete(students.First());
//unitOfWorkRepository.SaveChanges();


四,总结

上面的设计,把Repository的通用代码剥离到父类中,同时又允许每个Repository扩展自己的方法,达到了比较理想的状态。

只是现在的设计和Autofac耦合了,但是想剥离Autofac的话,直接使用下面的方式获取IStudentRepository的实例就很困难。

unitOfWorkRepository.GetRepository<IStudentRepository>();

如果有什么好的办法,欢迎指教。也欢迎各位拍砖。

最后,附上本文的相关源代码. RepositoryDesign.zip
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐