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

相关文章

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

Python GUI框架中的PyQt详解

《PythonGUI框架中的PyQt详解》PyQt是Python语言中最强大且广泛应用的GUI框架之一,基于Qt库的Python绑定实现,本文将深入解析PyQt的核心模块,并通过代码示例展示其应用场... 目录一、PyQt核心模块概览二、核心模块详解与示例1. QtCore - 核心基础模块2. QtWid

最新Spring Security实战教程之Spring Security安全框架指南

《最新SpringSecurity实战教程之SpringSecurity安全框架指南》SpringSecurity是Spring生态系统中的核心组件,提供认证、授权和防护机制,以保护应用免受各种安... 目录前言什么是Spring Security?同类框架对比Spring Security典型应用场景传统

Python结合Flask框架构建一个简易的远程控制系统

《Python结合Flask框架构建一个简易的远程控制系统》这篇文章主要为大家详细介绍了如何使用Python与Flask框架构建一个简易的远程控制系统,能够远程执行操作命令(如关机、重启、锁屏等),还... 目录1.概述2.功能使用系统命令执行实时屏幕监控3. BUG修复过程1. Authorization

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

Gin框架中的GET和POST表单处理的实现

《Gin框架中的GET和POST表单处理的实现》Gin框架提供了简单而强大的机制来处理GET和POST表单提交的数据,通过c.Query、c.PostForm、c.Bind和c.Request.For... 目录一、GET表单处理二、POST表单处理1. 使用c.PostForm获取表单字段:2. 绑定到结

修改若依框架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核