ABP框架—后台:仓储Repository(8)

2023-12-18 11:58

本文主要是介绍ABP框架—后台:仓储Repository(8),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在DDD领域驱动设计中,仓储实现了对数据进行增删改查操作的代码。
(1)ABP里面已经为我们定义了默认仓储,里面已经包含基本的仓储方法(增删改查)。我们可以直接调用。


(2)当ABP的默认仓储不能满足业务时,我们可以在默认仓储上进行扩展。继承默认仓储接口IRepository,和继承默认仓储实现类PDRepositoryBase来进行扩展。


(3)如果在默认仓储上进行扩展,我要把仓储接口和仓储实现分离。仓储接口应放置于领域层(Core),仓储实现应该放置于基础设施层(EntityFrameworkCore)

一、默认仓储

1.默认仓储接口IRepository


在命名namespace空间下 Abp.Domain.Repositories,默认仓储接口IRepository<TEntity, TPrimaryKey> 

#region 程序集 Abp, Version=4.3.0.0, Culture=neutral, PublicKeyToken=null
// C:\Users\Administrator\.nuget\packages\abp\4.3.0\lib\netstandard2.0\Abp.dll
// Decompiled with ICSharpCode.Decompiler 3.1.0.3652
#endregion
using Abp.Dependency;
using Abp.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;namespace Abp.Domain.Repositories
{public interface IRepository<TEntity, TPrimaryKey> : IRepository, ITransientDependency where TEntity : class, IEntity<TPrimaryKey>{IQueryable<TEntity> GetAll();IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors);List<TEntity> GetAllList();Task<List<TEntity>> GetAllListAsync();List<TEntity> GetAllList(Expression<Func<TEntity, bool>> predicate);Task<List<TEntity>> GetAllListAsync(Expression<Func<TEntity, bool>> predicate);T Query<T>(Func<IQueryable<TEntity>, T> queryMethod);TEntity Get(TPrimaryKey id);Task<TEntity> GetAsync(TPrimaryKey id);TEntity Single(Expression<Func<TEntity, bool>> predicate);Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> predicate);TEntity FirstOrDefault(TPrimaryKey id);Task<TEntity> FirstOrDefaultAsync(TPrimaryKey id);TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate);Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);TEntity Load(TPrimaryKey id);TEntity Insert(TEntity entity);Task<TEntity> InsertAsync(TEntity entity);TPrimaryKey InsertAndGetId(TEntity entity);Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity);TEntity InsertOrUpdate(TEntity entity);Task<TEntity> InsertOrUpdateAsync(TEntity entity);TPrimaryKey InsertOrUpdateAndGetId(TEntity entity);Task<TPrimaryKey> InsertOrUpdateAndGetIdAsync(TEntity entity);TEntity Update(TEntity entity);Task<TEntity> UpdateAsync(TEntity entity);TEntity Update(TPrimaryKey id, Action<TEntity> updateAction);Task<TEntity> UpdateAsync(TPrimaryKey id, Func<TEntity, Task> updateAction);void Delete(TEntity entity);Task DeleteAsync(TEntity entity);void Delete(TPrimaryKey id);Task DeleteAsync(TPrimaryKey id);void Delete(Expression<Func<TEntity, bool>> predicate);Task DeleteAsync(Expression<Func<TEntity, bool>> predicate);int Count();Task<int> CountAsync();int Count(Expression<Func<TEntity, bool>> predicate);Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate);long LongCount();Task<long> LongCountAsync();long LongCount(Expression<Func<TEntity, bool>> predicate);Task<long> LongCountAsync(Expression<Func<TEntity, bool>> predicate);}
}


2.默认仓储实现类PDRepositoryBase


在命名空间namespace下 EntityFrameworkCore.Repositories,默认仓储实现PDRepositoryBase<TEntity, TPrimaryKey> 

using Abp.Domain.Entities;
using Abp.Domain.Repositories;
using Abp.EntityFrameworkCore;
using Abp.EntityFrameworkCore.Repositories;namespace PD.EntityFrameworkCore.Repositories
{public abstract class PDRepositoryBase<TEntity, TPrimaryKey> : EfCoreRepositoryBase<PDDbContext, TEntity, TPrimaryKey>where TEntity : class, IEntity<TPrimaryKey>{protected PDRepositoryBase(IDbContextProvider<PDDbContext> dbContextProvider): base(dbContextProvider){}}public abstract class PDRepositoryBase<TEntity> : PDRepositoryBase<TEntity, int>, IRepository<TEntity>where TEntity : class, IEntity<int>{protected PDRepositoryBase(IDbContextProvider<PDDbContext> dbContextProvider): base(dbContextProvider){}}
}

对于如何调用默认仓储,后续文章会有说明


二、自定义仓储


1.定义仓储接口PDRepositoryBase

using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;namespace PD.Menu.Repository
{public interface IMenuRepository : IRepository<Sys_Menu, int>{Task<List<Sys_Menu>> GetSys_MenuList();}
}

2.仓储实现类MenuRepository

using Abp.EntityFrameworkCore;
using PD.EntityFrameworkCore.Repositories;
using PD.Menu;
using PD.Menu.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace PD.EntityFrameworkCore.MenuRepository
{public class MenuRepository : PDRepositoryBase<Sys_Menu, int>, IMenuRepository{public MenuRepository(IDbContextProvider<PDDbContext> dbContextProvider) : base(dbContextProvider){}public async Task<List<Sys_Menu>> GetSys_MenuList(){//GetAllListAsync()为默认仓储中的方法,可直接使用var query = await GetAllListAsync();//TODO.......return query;}}
}

说明:ABP中使用了依赖注入容器Castle.Windsor,因此构造函数中的注入对象由容器自动注入

dbContextProvider对象由容器Castle.Windsor自动注入

public MenuRepository(IDbContextProvider<PDDbContext> dbContextProvider) : base(dbContextProvider)
 {

  }

容器Castle.Windsor配置位于Startup启动类的ConfigureServices方法中

services.AddAbp<PDWebHostModule>(// Configure Log4Net loggingoptions => options.IocManager.IocContainer.AddFacility<LoggingFacility>(f => f.UseAbpLog4Net().WithConfig("log4net.config")));

,依赖注入后续学习中会继续讲解

这篇关于ABP框架—后台:仓储Repository(8)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/508290

相关文章

修改若依框架Token的过期时间问题

《修改若依框架Token的过期时间问题》本文介绍了如何修改若依框架中Token的过期时间,通过修改`application.yml`文件中的配置来实现,默认单位为分钟,希望此经验对大家有所帮助,也欢迎... 目录修改若依框架Token的过期时间修改Token的过期时间关闭Token的过期时js间总结修改若依

Linux使用nohup命令在后台运行脚本

《Linux使用nohup命令在后台运行脚本》在Linux或类Unix系统中,后台运行脚本是一项非常实用的技能,尤其适用于需要长时间运行的任务或服务,本文我们来看看如何使用nohup命令在后台... 目录nohup 命令简介基本用法输出重定向& 符号的作用后台进程的特点注意事项实际应用场景长时间运行的任务服

MyBatis框架实现一个简单的数据查询操作

《MyBatis框架实现一个简单的数据查询操作》本文介绍了MyBatis框架下进行数据查询操作的详细步骤,括创建实体类、编写SQL标签、配置Mapper、开启驼峰命名映射以及执行SQL语句等,感兴趣的... 基于在前面几章我们已经学习了对MyBATis进行环境配置,并利用SqlSessionFactory核

cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个?

跨平台系列 cross-plateform 跨平台应用程序-01-概览 cross-plateform 跨平台应用程序-02-有哪些主流技术栈? cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个? cross-plateform 跨平台应用程序-04-React Native 介绍 cross-plateform 跨平台应用程序-05-Flutte

Spring框架5 - 容器的扩展功能 (ApplicationContext)

private static ApplicationContext applicationContext;static {applicationContext = new ClassPathXmlApplicationContext("bean.xml");} BeanFactory的功能扩展类ApplicationContext进行深度的分析。ApplicationConext与 BeanF

数据治理框架-ISO数据治理标准

引言 "数据治理"并不是一个新的概念,国内外有很多组织专注于数据治理理论和实践的研究。目前国际上,主要的数据治理框架有ISO数据治理标准、GDI数据治理框架、DAMA数据治理管理框架等。 ISO数据治理标准 改标准阐述了数据治理的标准、基本原则和数据治理模型,是一套完整的数据治理方法论。 ISO/IEC 38505标准的数据治理方法论的核心内容如下: 数据治理的目标:促进组织高效、合理地

ZooKeeper 中的 Curator 框架解析

Apache ZooKeeper 是一个为分布式应用提供一致性服务的软件。它提供了诸如配置管理、分布式同步、组服务等功能。在使用 ZooKeeper 时,Curator 是一个非常流行的客户端库,它简化了 ZooKeeper 的使用,提供了高级的抽象和丰富的工具。本文将详细介绍 Curator 框架,包括它的设计哲学、核心组件以及如何使用 Curator 来简化 ZooKeeper 的操作。 1

【Kubernetes】K8s 的安全框架和用户认证

K8s 的安全框架和用户认证 1.Kubernetes 的安全框架1.1 认证:Authentication1.2 鉴权:Authorization1.3 准入控制:Admission Control 2.Kubernetes 的用户认证2.1 Kubernetes 的用户认证方式2.2 配置 Kubernetes 集群使用密码认证 Kubernetes 作为一个分布式的虚拟

Spring Framework系统框架

序号表示的是学习顺序 IoC(控制反转)/DI(依赖注入): ioc:思想上是控制反转,spring提供了一个容器,称为IOC容器,用它来充当IOC思想中的外部。 我的理解就是spring把这些对象集中管理,放在容器中,这个容器就叫Ioc这些对象统称为Bean 用对象的时候不用new,直接外部提供(bean) 当外部的对象有关系的时候,IOC给它俩绑好(DI) DI和IO

Sentinel 高可用流量管理框架

Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。 Sentinel 具有以下特性: 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应