abp(net core)+easyui+efcore仓储系统目录

abp(net core)+easyui+efcore仓储系统——ABP总体介绍(一)
<https://www.cnblogs.com/chillsrc/p/10913047.html>

abp(net core)+easyui+efcore仓储系统——解决方案介绍(二)
<https://www.cnblogs.com/chillsrc/p/10944870.html>

abp(net core)+easyui+efcore仓储系统——领域层创建实体(三)
<https://www.cnblogs.com/chillsrc/p/10980974.html>

 

在上一篇文章中我们创建了Module实体并在数据库中生成了数据表,在这一文章中我们介绍如何来对数据库进行操作。

一、先来介绍下仓储

仓储(Repository): 仓储用来操作数据库进行数据存取。仓储接口在领域层定义,而仓储的实现类应该写在基础设施层。

在ABP中,仓储类要实现IRepository接口,接口定义了常用的增删改查以及聚合方法,其中包括同步及异步方法。主要包括以下方法:

 

ABP针对不同的ORM框架对这个接口进行了默认的实现:

    1) 对于EntityFrameworkCore,提供了EfCoreRepositoryBase<TDbContext, TEntity,
TPrimaryKey>的泛型版本的实现方式。
    2) 对于NHibernate,提供了NhRepositoryBase<TEntity, TPrimaryKey>的泛型版本的实现方式。

     
这些使用泛型的实现类,在大多数的时候,这些实现类中的方法已经足够应付一般增删改查的需要。如果默认实现类中的方法对于实体类来说已够用了,我们便不需要再去创建这个实体所需的仓储接口与实现类。直接通过在应用服务层定义仓储引用,然后通过构造函数注入即可。

接下来,我们来讲讲如何自定义仓储实现类。

二、如何实现自定义仓储

1. 在Visual Studio 2017的“解决方案资源管理器”中,右键单击在领域层“ABP.TPLMS.Core”项目。 选择“添加” >
“新建文件夹”。

2.将文件夹命名为“IRepositories”。

3. 右键单击“IRepositories”文件夹,然后选择“添加” > “类”。 在弹出对话框“添加新项-
ABP.TPLMS.Core”中选择接口,将接口命名为 IModuleRepository。然后使用鼠标左键单击“添加”按钮。如下图。

 

4. IModuleRepository接口的代码如下。

using Abp.Domain.Repositories; using ABP.TPLMS.Entitys; using System; using
System.Collections.Generic;using System.Text; namespace ABP.TPLMS.IRepositories
{interface IModuleRepository: IRepository<Module> { /// <summary> /// 分页查询功能模块
/// </summary> /// <param name="pageindex">页索引</param> /// <param
name="pagesize">每页多少条</param> /// <returns>模块列表</returns> IEnumerable<Module>
LoadModules(int pageindex, int pagesize); /// <summary> /// 批量删除 /// </summary>
/// <param name="ids"></param> /// <returns></returns> bool Delete(string ids);
} }
 

三、在基础架构层,实现该仓储。

    1. 在Visual Studio
2017的“解决方案资源管理器”中,打开“ABP.TPLMS.EntityFrameworkCore”项目,找到“Repositories”目录,在这个目录中有一个ABP生成的基类。如下图。

 

    2. 鼠标右键单击“Repositories”文件夹,然后选择“添加” > “类”。 在弹出对话框“添加新项-
ABP.TPLMS.EntityFrameworkCore”中类命名为 IModuleRepository。然后使用鼠标左键单击“添加”按钮。代码如下。

using Abp.EntityFrameworkCore; using ABP.TPLMS.Entitys; using
ABP.TPLMS.IRepositories;using System; using System.Collections.Generic; using
System.Linq;using System.Linq.Expressions; using System.Text; namespace
ABP.TPLMS.EntityFrameworkCore.Repositories {public class
ModuleRepository:TPLMSRepositoryBase<Module>, IModuleRepository { public
ModuleRepository(IDbContextProvider<TPLMSDbContext> dbContextProvider) : base
(dbContextProvider) { }public bool Delete(string ids) { var idList = ids.Split('
,'); Expression<Func<Module, bool>> exp = m => idList.Contains(m.Id.ToString());
bool result = true; Delete(exp); return result; } public IEnumerable<Module>
LoadModules(int pageindex, int pagesize) { return Context.Modules.OrderBy(u =>
u.Id).Skip((pageindex -1) * pagesize).Take(pagesize); } } }
     
该仓储实现,继承自模板生成的TPLMSRepositoryBase泛型抽象类,然后再实现IModuleRepository接口。这里要显示声明实现类的有参构造函数,使用泛型的IDbContextProvider将数据库上下文的子类ChargeStationContext传给父类的构造函数。如下图。

 

四、仓储的注意事项

* 仓储实现类方法中,ABP自动进行数据库连接的开启和关闭。
* 仓储方法被调用时,数据库连接自动开启事务。
* 当仓储方法调用另外一个仓储的方法,它们实际上共享的是同一个数据库连接和事务。
*
仓储对象都是暂时性的,因为IRepository接口默认继承自ITransientDependency接口。所以,仓储对象只有在需要注入的时候,才会由Ioc容器自动创建新实例。
* ABP默认的泛型仓储功能满足我们大部分的CURD操作需求。当有默认的仓储功能不满足需求的情况下,可以创建自己定制化的仓储实现类。

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信