WPF实战学习笔记09-创建工作单元

news2024/12/24 10:21:53

创建工作单元

添加包

  • Microsoft.EntityFrameworkCore.AutoHistory

    A plugin for Microsoft.EntityFrameworkCore to support automatically recording data changes history.

  • Microsoft.EntityFrameworkCore.UnitOfWork

    A plugin for Microsoft.EntityFrameworkCore to support repository, unit of work patterns, and multiple database with distributed transaction supported.

依赖注入

image-20230712142942894

添加命名空间using Arch.EntityFrameworkCore.UnitOfWork;

添加仓储段

添加文件

  • webapi工程

./Repository/MemoRepository.cs

./Repository/ToDoRepository.cs

./Repository/UserRepository.cs

实现仓储段接口

注意要继承Repository, IRepository两个以及接口

MemoRepository.cs
using Arch.EntityFrameworkCore.UnitOfWork;
using Arch.EntityFrameworkCore.UnitOfWork.Collections;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Query;
using MyToDo.Api.Context;
using System.Linq.Expressions;

namespace MyToDo.Api.Repository
{
    public class MemoRepository : Repository<MyTodoContext>, IRepository<Memo>
    {
        public MemoRepository(MyTodoContext dbContext) : base(dbContext)
        {
        }

        public int Count(Expression<Func<Memo, bool>> predicate = null)
        {
            throw new NotImplementedException();
        }

        public void Delete(Memo entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(params Memo[] entities)
        {
            throw new NotImplementedException();
        }

        public void Delete(IEnumerable<Memo> entities)
        {
            throw new NotImplementedException();
        }

        public Memo GetFirstOrDefault(Expression<Func<Memo, bool>> predicate = null, Func<IQueryable<Memo>, IOrderedQueryable<Memo>> orderBy = null, Func<IQueryable<Memo>, IIncludableQueryable<Memo, object>> include = null, bool disableTracking = true, bool ignoreQueryFilters = false)
        {
            throw new NotImplementedException();
        }

        public TResult GetFirstOrDefault<TResult>(Expression<Func<Memo, TResult>> selector, Expression<Func<Memo, bool>> predicate = null, Func<IQueryable<Memo>, IOrderedQueryable<Memo>> orderBy = null, Func<IQueryable<Memo>, IIncludableQueryable<Memo, object>> include = null, bool disableTracking = true, bool ignoreQueryFilters = false)
        {
            throw new NotImplementedException();
        }

        public Task<TResult> GetFirstOrDefaultAsync<TResult>(Expression<Func<Memo, TResult>> selector, Expression<Func<Memo, bool>> predicate = null, Func<IQueryable<Memo>, IOrderedQueryable<Memo>> orderBy = null, Func<IQueryable<Memo>, IIncludableQueryable<Memo, object>> include = null, bool disableTracking = true, bool ignoreQueryFilters = false)
        {
            throw new NotImplementedException();
        }

        public Task<Memo> GetFirstOrDefaultAsync(Expression<Func<Memo, bool>> predicate = null, Func<IQueryable<Memo>, IOrderedQueryable<Memo>> orderBy = null, Func<IQueryable<Memo>, IIncludableQueryable<Memo, object>> include = null, bool disableTracking = true, bool ignoreQueryFilters = false)
        {
            throw new NotImplementedException();
        }

        public IPagedList<Memo> GetPagedList(Expression<Func<Memo, bool>> predicate = null, Func<IQueryable<Memo>, IOrderedQueryable<Memo>> orderBy = null, Func<IQueryable<Memo>, IIncludableQueryable<Memo, object>> include = null, int pageIndex = 0, int pageSize = 20, bool disableTracking = true, bool ignoreQueryFilters = false)
        {
            throw new NotImplementedException();
        }

        public IPagedList<TResult> GetPagedList<TResult>(Expression<Func<Memo, TResult>> selector, Expression<Func<Memo, bool>> predicate = null, Func<IQueryable<Memo>, IOrderedQueryable<Memo>> orderBy = null, Func<IQueryable<Memo>, IIncludableQueryable<Memo, object>> include = null, int pageIndex = 0, int pageSize = 20, bool disableTracking = true, bool ignoreQueryFilters = false) where TResult : class
        {
            throw new NotImplementedException();
        }

        public Task<IPagedList<Memo>> GetPagedListAsync(Expression<Func<Memo, bool>> predicate = null, Func<IQueryable<Memo>, IOrderedQueryable<Memo>> orderBy = null, Func<IQueryable<Memo>, IIncludableQueryable<Memo, object>> include = null, int pageIndex = 0, int pageSize = 20, bool disableTracking = true, CancellationToken cancellationToken = default, bool ignoreQueryFilters = false)
        {
            throw new NotImplementedException();
        }

        public Task<IPagedList<TResult>> GetPagedListAsync<TResult>(Expression<Func<Memo, TResult>> selector, Expression<Func<Memo, bool>> predicate = null, Func<IQueryable<Memo>, IOrderedQueryable<Memo>> orderBy = null, Func<IQueryable<Memo>, IIncludableQueryable<Memo, object>> include = null, int pageIndex = 0, int pageSize = 20, bool disableTracking = true, CancellationToken cancellationToken = default, bool ignoreQueryFilters = false) where TResult : class
        {
            throw new NotImplementedException();
        }

        public void Insert(Memo entity)
        {
            throw new NotImplementedException();
        }

        public void Insert(params Memo[] entities)
        {
            throw new NotImplementedException();
        }

        public void Insert(IEnumerable<Memo> entities)
        {
            throw new NotImplementedException();
        }

        public ValueTask<EntityEntry<Memo>> InsertAsync(Memo entity, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public Task InsertAsync(params Memo[] entities)
        {
            throw new NotImplementedException();
        }

        public Task InsertAsync(IEnumerable<Memo> entities, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public void Update(Memo entity)
        {
            throw new NotImplementedException();
        }

        public void Update(params Memo[] entities)
        {
            throw new NotImplementedException();
        }

        public void Update(IEnumerable<Memo> entities)
        {
            throw new NotImplementedException();
        }

        Memo IRepository<Memo>.Find(params object[] keyValues)
        {
            throw new NotImplementedException();
        }

        ValueTask<Memo> IRepository<Memo>.FindAsync(params object[] keyValues)
        {
            throw new NotImplementedException();
        }

        ValueTask<Memo> IRepository<Memo>.FindAsync(object[] keyValues, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        IQueryable<Memo> IRepository<Memo>.FromSql(string sql, params object[] parameters)
        {
            throw new NotImplementedException();
        }

        IQueryable<Memo> IRepository<Memo>.GetAll()
        {
            throw new NotImplementedException();
        }
    }
}

ToDoRepository.cs
using Arch.EntityFrameworkCore.UnitOfWork;
using Microsoft.EntityFrameworkCore;
using MyToDo.Api.Context;

namespace MyToDo.Api.Repository
{
    public class ToDoRepository : Repository<Todo>, IRepository<Todo>
    {
        public ToDoRepository(MyTodoContext dbContext) : base(dbContext)
        {
        }
    }
}

User
using Arch.EntityFrameworkCore.UnitOfWork;
using Arch.EntityFrameworkCore.UnitOfWork.Collections;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Query;
using MyToDo.Api.Context;
using MyToDo.Api.Migrations;
using System.Linq.Expressions;

namespace MyToDo.Api.Repository
{
    public class UserRepository : Repository<MyTodoContext>, IRepository<User>
    {
        public UserRepository(MyTodoContext dbContext) : base(dbContext)
        {
        }

        public int Count(Expression<Func<User, bool>> predicate = null)
        {
            throw new NotImplementedException();
        }

        public void Delete(User entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(params User[] entities)
        {
            throw new NotImplementedException();
        }

        public void Delete(IEnumerable<User> entities)
        {
            throw new NotImplementedException();
        }

        public User GetFirstOrDefault(Expression<Func<User, bool>> predicate = null, Func<IQueryable<User>, IOrderedQueryable<User>> orderBy = null, Func<IQueryable<User>, IIncludableQueryable<User, object>> include = null, bool disableTracking = true, bool ignoreQueryFilters = false)
        {
            throw new NotImplementedException();
        }

        public TResult GetFirstOrDefault<TResult>(Expression<Func<User, TResult>> selector, Expression<Func<User, bool>> predicate = null, Func<IQueryable<User>, IOrderedQueryable<User>> orderBy = null, Func<IQueryable<User>, IIncludableQueryable<User, object>> include = null, bool disableTracking = true, bool ignoreQueryFilters = false)
        {
            throw new NotImplementedException();
        }

        public Task<TResult> GetFirstOrDefaultAsync<TResult>(Expression<Func<User, TResult>> selector, Expression<Func<User, bool>> predicate = null, Func<IQueryable<User>, IOrderedQueryable<User>> orderBy = null, Func<IQueryable<User>, IIncludableQueryable<User, object>> include = null, bool disableTracking = true, bool ignoreQueryFilters = false)
        {
            throw new NotImplementedException();
        }

        public Task<User> GetFirstOrDefaultAsync(Expression<Func<User, bool>> predicate = null, Func<IQueryable<User>, IOrderedQueryable<User>> orderBy = null, Func<IQueryable<User>, IIncludableQueryable<User, object>> include = null, bool disableTracking = true, bool ignoreQueryFilters = false)
        {
            throw new NotImplementedException();
        }

        public IPagedList<User> GetPagedList(Expression<Func<User, bool>> predicate = null, Func<IQueryable<User>, IOrderedQueryable<User>> orderBy = null, Func<IQueryable<User>, IIncludableQueryable<User, object>> include = null, int pageIndex = 0, int pageSize = 20, bool disableTracking = true, bool ignoreQueryFilters = false)
        {
            throw new NotImplementedException();
        }

        public IPagedList<TResult> GetPagedList<TResult>(Expression<Func<User, TResult>> selector, Expression<Func<User, bool>> predicate = null, Func<IQueryable<User>, IOrderedQueryable<User>> orderBy = null, Func<IQueryable<User>, IIncludableQueryable<User, object>> include = null, int pageIndex = 0, int pageSize = 20, bool disableTracking = true, bool ignoreQueryFilters = false) where TResult : class
        {
            throw new NotImplementedException();
        }

        public Task<IPagedList<User>> GetPagedListAsync(Expression<Func<User, bool>> predicate = null, Func<IQueryable<User>, IOrderedQueryable<User>> orderBy = null, Func<IQueryable<User>, IIncludableQueryable<User, object>> include = null, int pageIndex = 0, int pageSize = 20, bool disableTracking = true, CancellationToken cancellationToken = default, bool ignoreQueryFilters = false)
        {
            throw new NotImplementedException();
        }

        public Task<IPagedList<TResult>> GetPagedListAsync<TResult>(Expression<Func<User, TResult>> selector, Expression<Func<User, bool>> predicate = null, Func<IQueryable<User>, IOrderedQueryable<User>> orderBy = null, Func<IQueryable<User>, IIncludableQueryable<User, object>> include = null, int pageIndex = 0, int pageSize = 20, bool disableTracking = true, CancellationToken cancellationToken = default, bool ignoreQueryFilters = false) where TResult : class
        {
            throw new NotImplementedException();
        }

        public void Insert(User entity)
        {
            throw new NotImplementedException();
        }

        public void Insert(params User[] entities)
        {
            throw new NotImplementedException();
        }

        public void Insert(IEnumerable<User> entities)
        {
            throw new NotImplementedException();
        }

        public ValueTask<EntityEntry<User>> InsertAsync(User entity, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public Task InsertAsync(params User[] entities)
        {
            throw new NotImplementedException();
        }

        public Task InsertAsync(IEnumerable<User> entities, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public void Update(User entity)
        {
            throw new NotImplementedException();
        }

        public void Update(params User[] entities)
        {
            throw new NotImplementedException();
        }

        public void Update(IEnumerable<User> entities)
        {
            throw new NotImplementedException();
        }

        User IRepository<User>.Find(params object[] keyValues)
        {
            throw new NotImplementedException();
        }

        ValueTask<User> IRepository<User>.FindAsync(params object[] keyValues)
        {
            throw new NotImplementedException();
        }

        ValueTask<User> IRepository<User>.FindAsync(object[] keyValues, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        IQueryable<User> IRepository<User>.FromSql(string sql, params object[] parameters)
        {
            throw new NotImplementedException();
        }

        IQueryable<User> IRepository<User>.GetAll()
        {
            throw new NotImplementedException();
        }
    }
}

添加测试数据

1. 数据库添加测试数据

2. 编写测试程序

  • WeatherForecastController(覆盖到对应的类中)

    private readonly IUnitOfWork uow;
    
    public WeatherForecastController(ILogger<WeatherForecastController> logger,IUnitOfWork unitOfWork)
    {
        _logger = logger;
        this.uow = unitOfWork;
    }
    public IEnumerable<WeatherForecast> Get()
    {
        //获取用户表
        var service= uow.GetRepository<User>();
        var res = service.GetAll();
        var rng = new Random();
    }
    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/789562.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Docker 实用技巧】动态切换国内外源

目录 前言&#xff1a; 1、背景 2、ChatGPT 的方案 3、我的方案 3.1 本地构建 3.2 Github Action 构建 3.3 实际方案 前言&#xff1a; Docker 是一个非常流行的开发和部署平台&#xff0c;它可以帮助开发者快速构建、部署和管理应用程序。在使用 Docker 的过程中&…

聊聊STM32 ADC的话题

STM32 微控制器系列提供了多个模拟数字转换器&#xff08;ADC&#xff09;模块&#xff0c;用于实现模拟信号的采集和转换为数字信号。ADC 在很多应用中都是非常重要的&#xff0c;例如传感器数据采集、电压测量等。 在 STM32 中&#xff0c;ADC 可以通过 STM32HAL 库提供的函…

Unity游戏源码分享-3d机器人推箱子游戏

Unity游戏源码分享-3d机器人推箱子游戏 一个非常意思的3D游戏 工程地址&#xff1a;https://download.csdn.net/download/Highning0007/88098014

leetcode5. 最长回文子串(Manacher - java)

Manacher回文算法 leetcode5. 最长回文子串Manacher 算法 manacher 算法 leetcode5. 最长回文子串 给你一个字符串 s&#xff0c;找到 s 中最长的回文子串。 如果字符串的反序与原始字符串相同&#xff0c;则该字符串称为回文字符串。 示例 1&#xff1a; 输入&#xff1a;s “…

pear文件利用 (远程文件下载、生成配置文件、写配置文件) 从一道题看——CTFshow私教 web40

web40 考点&#xff1a;pear文件包含 pear是PHP的一个扩展 条件&#xff1a; 1 有文件包含点 2 开启了pear扩展 &#xff08;可以当他是一个框架&#xff09; 3 配置文件中register_argc_argv 设置为On,而默认为Off&#xff08;$_SERVER[‘argv’]生效&#xff09; 4 找到…

互联网创业项目整合:提高成功率的关键方法

互联网创业是当前非常火热的话题&#xff0c;但是市场竞争也异常激烈&#xff0c;成功率不高。今天&#xff0c;我来分享一些互联网创业项目整合的方法&#xff0c;帮助大家创造更大的机会。 1️⃣ 选择适合自己的领域 在选择创业项目时&#xff0c;一定要根据自己的专业、兴…

kafka权威指南学习

0、kafka常用命令 Kafka是一个分布式流处理平台&#xff0c;它具有高度可扩展性和容错性。以下是Kafka最新版本中常用的一些命令&#xff1a; 创建一个主题&#xff08;topic&#xff09;&#xff1a; bin/kafka-topics.sh --create --topic my-topic --partitions 3 --replic…

【Nodejs】Express基本使用

Express 中文网 基于 Node.js 平台&#xff0c;快速、开放、极简的 web 开发框架。 1.Express的安装方式 Express的安装可直接使用npm包管理器上的项目&#xff0c;在安装npm之前可先安装淘宝镜像&#xff1a; npm install -g cnpm --registryhttps://registry.npmmirror.com/…

安全渗透--正则表达式

什么是正则表达式&#xff1f; 正则表达式是一组由字母和符号组成的特殊文本&#xff0c;它可以用来从文本中找出满足你想要的格式的句子。 一个正则表达式是一种从左到右匹配主体字符串的模式。 “Regular expression”这个词比较拗口&#xff0c;我们常使用缩写的术语“regex…

【高危】Apache ShardingSphere 反序列化漏洞

漏洞描述 Apache ShardingSphere 是一个开源的分布式数据库中间件。 在 Apache ShardingSphere 受影响版本中&#xff0c;由于 Apache ShardingSphere-Agent 在反序列化 YAML 配置文件时未正确验证不受信任数据&#xff0c;攻击者可以利用 SnakeYAML 反序列化 java.net.URLCl…

阿里云盘自动每日签到无需部署无需服务器(仅限学习交流使用)

一、前言 阿里云盘自动每日签到&#xff0c;无需部署&#xff0c;无需服务器 执行思路&#xff1a;使用金山文档的每日定时任务&#xff0c;执行阿里云盘签到接口。 二、效果展示&#xff1a; 三、步骤&#xff1a; 1、进入金山文档网页版 金山文档官网&#xff1a;https:…

【C++】STL——list的介绍和使用、list增删查改函数的介绍和使用、push_back、pop_back

文章目录 1.list的使用2.list的增删查改函数&#xff08;1&#xff09;push_front 在list首元素前插入值为val的元素&#xff08;2&#xff09;pop_front 删除list中第一个元素&#xff08;3&#xff09;push_back 在list尾部插入值为val的元素&#xff08;4&#xff09;pop_ba…

【深度学习笔记】动量梯度下降法

本专栏是网易云课堂人工智能课程《神经网络与深度学习》的学习笔记&#xff0c;视频由网易云课堂与 deeplearning.ai 联合出品&#xff0c;主讲人是吴恩达 Andrew Ng 教授。感兴趣的网友可以观看网易云课堂的视频进行深入学习&#xff0c;视频的链接如下&#xff1a; 神经网络和…

Asp.Net 6中使用Log4Net

Asp.Net 6中使用Log4Net 1. 先新建一个ASP.NET Core空项目 2. 通过Nuget包管理器安装下面两个包 log4net Microsoft.Extensions.Logging.Log4Net.AspNetCore 3. 在项目根目录下新建log4net的配置文件log4net.config&#xff0c;并将其设置为始终复制。 <?xml version&quo…

最新Ai创作源码ChatGPT商用运营源码/支持GPT4.0+支持ai绘画+支持Mind思维导图生成

本系统使用Nestjs和Vue3框架技术&#xff0c;持续集成AI能力到本系统&#xff01; 支持GPT3模型、GPT4模型Midjourney专业绘画&#xff08;全自定义调参&#xff09;、Midjourney以图生图、Dall-E2绘画Mind思维导图生成应用工作台&#xff08;Prompt&#xff09;AI绘画广场自定…

高效复用:RecyclerView Item中嵌套列表时的优化技巧

文章目录 背景实现思路方式一方式二方式三 性能对比总结 背景 如上面截图中的效果&#xff0c;首先这是一个多样式的滑动列表&#xff08;截图里只列举了其中的3 种样式&#xff09;&#xff0c;整体使用 RecyclerView 来实现毋庸置疑。接下来要探讨的是截图中第3个ItemView 中…

Linux系列---【CentOS 7通过MSTSC连接远程桌面】

安装对应的yum源 yum list lightdm xorgxrdp xrdp 可以看到这些软件都在epel中&#xff0c;如果没有的话&#xff0c;请先安装对应的yum源。命令如下&#xff1a; yum install -y epel-release 确认yum源没有问题之后&#xff0c;我们就可以进行安装了。 安装lightdm xorgxrdp…

QT【day1】

登录框&#xff1a; #include "mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) {//窗口设置this->setFixedSize(600,600); //大小this->setWindowTitle("MUMU"); //文本内容this->setWindowOpacity(0.8); //透…

Java五大内存区域介绍与常见异常

jvm虚拟机在执行java程序的过程中会把它所管理的内存划分为若干个不同的区域。这些区域各自有各自的用途&#xff0c;以及创建和销毁的时间&#xff0c;有的区域随着虚拟机进程的启动而存在&#xff0c;有的区域则依赖用户进程的启动和结束而建立和销毁。 1、程序计数器 程序计…

《动手学深度学习》——2.6概率(模拟掷色子代码解析)

文章目录 完整代码代码解析结果展示 完整代码 %matplotlib inline !pip install d2l import torch from torch.distributions import multinomial from d2l import torch as d2l fair_probs torch.ones([6]) / 6 counts multinomial.Multinomial(10, fair_probs).sample((50…