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

创建工作单元

添加包

  • 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.

依赖注入

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

添加仓储段

添加文件

  • webapi工程

./Repository/MemoRepository.cs

./Repository/ToDoRepository.cs

./Repository/UserRepository.cs

实现仓储段接口

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

MemoRepository.cs
c# 复制代码
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
c# 复制代码
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
c# 复制代码
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(覆盖到对应的类中)

    c# 复制代码
    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();
    }
相关推荐
xiaocao_102341 分钟前
手机备忘录:安全存储与管理个人笔记的理想选择
笔记·安全·智能手机
索然无味io1 小时前
XML外部实体注入--漏洞利用
xml·前端·笔记·学习·web安全·网络安全·php
一弓虽1 小时前
java基础学习——jdbc基础知识详细介绍
java·学习·jdbc·连接池
王磊鑫1 小时前
Java入门笔记(1)
java·开发语言·笔记
安冬的码畜日常2 小时前
【Vim Masterclass 笔记22】S09L40 + L41:同步练习11:Vim 的配置与 vimrc 文件的相关操作(含点评课内容)
笔记·vim·vim配置·vim同步练习·vim options·vim option-list
五味香2 小时前
Java学习,List 元素替换
android·java·开发语言·python·学习·golang·kotlin
追Star仙3 小时前
基于Qt中的QAxObject实现指定表格合并数据进行word表格的合并
开发语言·笔记·qt·word
Clockwiseee3 小时前
docker学习
学习·docker·eureka
安冬的码畜日常4 小时前
【Vim Masterclass 笔记24】S10L43 + L44:同步练习10 —— 基于 Vim 缓冲区的各类基础操作练习(含点评课)
笔记·vim·自学笔记·vim同步练习·vim缓冲区·vim buffer·vim缓冲区练习
lozhyf4 小时前
Go语言-学习一
开发语言·学习·golang