您现在的位置是:网站首页> 编程资料编程资料
ASP.Net Core基于EF6、Unitwork、Autofac实现Repository模式_实用技巧_
2023-05-24
272人已围观
简介 ASP.Net Core基于EF6、Unitwork、Autofac实现Repository模式_实用技巧_
一、实现的思路和结构图
- Repository的共同性
有一些公共的方法(增删改查), 这些方法无关于Repository操作的是哪个实体类,可以把这些方法定义成接口IRepository,然后有个基类BaseRepository实现该接口的方法。常见的方法,比如Find, Filter, Delete, Create等
- Repository的差异性
每个Repository类又会有一些差异性,应当允许它们能够继承BaseRepository之外,还能够再扩展自己的一些方法。所以每个类都可以再定义一个自己特有的接口,定义一些属于自己Repository的方法。
- Repository的协同性
不同的Repository可能需要协同,Repository对数据的修改,需要在统一的保存.
最终实现的类结构图如下:

二、Repository设计具体的实现代码
IRepository接口定义了Repository共有的方法, BaseRepository实现了这些接口的方法。其它的Repository类再集成BaseRepository方法,就天然的得到了对数据操作的基本方法。
- IRepository代码
public interface IRepositorywhere TEntity : class { /// /// Gets all objects from database /// ///IQueryable All(); /// /// Gets objects from database by filter. /// /// Specified a filter ///IQueryable Filter(Expression > predicate); /// /// Gets objects from database with filtering and paging. /// /// Specified a filter /// Returns the total records count of the filter. /// Specified the page index. /// Specified the page size ///IQueryable Filter(Expression > filter, out int total, int index = 0, int size = 50); /// /// Gets the object(s) is exists in database by specified filter. /// /// Specified the filter expression ///bool Contains(Expression > predicate); /// /// Find object by keys. /// /// Specified the search keys. ///TEntity Find(params object[] keys); /// /// Find object by specified expression. /// /// ///TEntity Find(Expression > predicate); /// /// Create a new object to database. /// /// Specified a new object to create. ///void Create(TEntity t); /// /// Delete the object from database. /// /// Specified a existing object to delete. void Delete(TEntity t); ////// Delete objects from database by specified filter expression. /// /// ///int Delete(Expression > predicate); /// /// Update object changes and save to database. /// /// Specified the object to save. ///void Update(TEntity t); /// /// Select Single Item by specified expression. /// /// ///TEntity FirstOrDefault(Expression > expression); }
- BaseRepository代码
public class BaseRepository: IRepository where TEntity : class { protected readonly DbContext Context; public BaseRepository(DbContext context) { Context = context; } /// /// Gets all objects from database /// ///public IQueryable All() { return Context.Set ().AsQueryable(); } /// /// Gets objects from database by filter. /// /// Specified a filter ///public virtual IQueryable Filter(Expression > predicate) { return Context.Set ().Where (predicate).AsQueryable (); } /// /// Gets objects from database with filtering and paging. /// /// Specified a filter /// Returns the total records count of the filter. /// Specified the page index. /// Specified the page size ///public virtual IQueryable Filter(Expression > filter, out int total, int index = 0, int size = 50) { var skipCount = index * size; var resetSet = filter != null ? Context.Set ().Where (filter).AsQueryable() : Context.Set ().AsQueryable(); resetSet = skipCount == 0 ? resetSet.Take(size) : resetSet.Skip(skipCount).Take(size); total = resetSet.Count(); return resetSet.AsQueryable(); } /// /// Gets the object(s) is exists in database by specified filter. /// /// Specified the filter expression ///public bool Contains(Expression > predicate) { return Context.Set ().Any(predicate); } /// /// Find object by keys. /// /// Specified the search keys. ///public virtual TEntity Find(params object[] keys) { return Context.Set ().Find(keys); } /// /// Find object by specified expression. /// /// ///public virtual TEntity Find(Expression > predicate) { return Context.Set ().FirstOrDefault (predicate); } /// /// Create a new object to database. /// /// Specified a new object to create. ///public virtual void Create(TEntity t) { Context.Set ().Add(t); } /// /// Delete the object from database. /// /// Specified a existing object to delete. public virtual void Delete(TEntity t) { Context.Set().Remove(t); } /// /// Delete objects from database by specified filter expression. /// /// ///public virtual int Delete(Expression > predicate) { var objects = Filter(predicate); foreach (var obj in objects) Context.Set ().Remove(obj); return Context.SaveChanges(); } /// /// Update object changes and save to database. /// /// Specified the object to save. ///public virtual void Update(TEntity t) { try { var entry = Context.Entry(t); Context.Set ().Attach(t); entry.State = EntityState.Modified; } catch (OptimisticConcurrencyException ex) { throw ex; } } /// /// Select Single Item by specified expression. /// /// ///public TEntity FirstOrDefault(Expression > expression) { return All().FirstOrDefault(expression); } }
IUnitOfWork接口定义了方法获取特定的Repository, 执行存储过程, SaveChange方法提交修改,统一更新数据。
- IUnitOfWork接口代码:
public interface IUnitOfWork : IDisposable { DbContext DbContext { get; } TRepository GetRepository() where TRepository : class; void ExecuteProcedure(string procedureCommand, params object[] sqlParams); void ExecuteSql(string sql); List SqlQuery(string sql); void SaveChanges(); } UnitOfWork代码, 代码中使用到了Autofac中的IComponentContext来获取Repository实例
public class UnitOfWork : IUnitOfWork { private readonly IComponentContext _componentContext; protected readonly DbContext Context; public UnitOfWork(DbContext context, IComponentContext componentContext) { Context = context; _componentContext = componentContext; } public DbContext DbContext => Context; public TRepository GetRepository() where TRepository : class { return _componentContext.Resolve(); } public void ExecuteProcedure(string procedureCommand, params object[] sqlParams) { Context.Database.ExecuteSqlCommand(procedureCommand, sqlParams); } public void ExecuteSql(string sql) { Context.Database.ExecuteSqlCommand(sql); } public List SqlQuery(string sql) { return Context.Database.SqlQuery(sql).ToList(); } public void SaveChanges() { try { Context.SaveChanges(); } catch (InvalidOperationException ex) { if (!ex.Message.Contains("The changes to the database were committed successfully")) { throw; } } } public void Dispose() { Context?.Dispose(); } } 三、Repository设计的具体的使用
这里我们定义一个
IStudentRepository接口, 包含了方法GetAllStudents(), 同时继承于IRepository接口
public interface IStudentRepository : IRepository{ IEnumerable GetAllStudents(); }
接着定义StudentRepository类来实现这个接口
public class StudentRepository : BaseRepository, IStudentRepository { private readonly SchoolContext _context; public StudentRepository(SchoolContext context) : base(context) { _context = context; } public IEnumerable GetAllStudents() { return _context.Students; } }
- 在
Application_Start方法中使用Autofac注册Repository的代码如下:
var builder = new ContainerBuilder(); //register controllers builder.RegisterControllers(typeof(MvcApplication).Assembly); //register repository builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces(); //add the Entity Framework context to make sure only one context per request builder.RegisterType().InstancePerR
相关内容
点击排行
本栏推荐
