发布:2023/12/7 15:37:50作者:大数据 来源:大数据 浏览次数:531
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public interface IBaseRepository<T> where T : class, new() { ValueTask<EntityEntry<T>> Insert(T entity); void Update(T entity); Task<int> Update(Expression<Func<T, bool>> whereLambda, Expression<Func<T, T>> entity); Task<int> Delete(Expression<Func<T, bool>> whereLambda); Task<bool> IsExist(Expression<Func<T, bool>> whereLambda); Task<T> GetEntity(Expression<Func<T, bool>> whereLambda); Task<List<T>> Select(); Task<List<T>> Select(Expression<Func<T, bool>> whereLambda); Task<Tuple<List<T>, int>> Select<S>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderByLambda, bool isAsc); } |
1 2 |
1 2 3 4 5 6 |
public interface IUnitOfWork { MyDbContext GetDbContext(); Task<int> SaveChangesAsync(); } |
1 2 |
1 2 3 |
public interface IStudentRepository : IBaseRepository<Student> { } |
1 2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
public class BaseRepository<T> where T : class, new() { private readonly MyDbContext myDbContext; public BaseRepository(MyDbContext myDbContext) { this.myDbContext = myDbContext; } public async ValueTask<EntityEntry<T>> Insert(T entity) { return await myDbContext.Set<T>().AddAsync(entity); } public void Update(T entity) { myDbContext.Set<T>().Update(entity); } public async Task<int> Update(Expression<Func<T, bool>> whereLambda, Expression<Func<T, T>> entity) { return await myDbContext.Set<T>().Where(whereLambda).UpdateAsync(entity); } public async Task<int> Delete(Expression<Func<T, bool>> whereLambda) { return await myDbContext.Set<T>().Where(whereLambda).DeleteAsync(); } public async Task<bool> IsExist(Expression<Func<T, bool>> whereLambda) { return await myDbContext.Set<T>().AnyAsync(whereLambda); } public async Task<T> GetEntity(Expression<Func<T, bool>> whereLambda) { return await myDbContext.Set<T>().AsNoTracking().FirstOrDefaultAsync(whereLambda); } public async Task<List<T>> Select() { return await myDbContext.Set<T>().ToListAsync(); } public async Task<List<T>> Select(Expression<Func<T, bool>> whereLambda) { return await myDbContext.Set<T>().Where(whereLambda).ToListAsync(); } public async Task<Tuple<List<T>, int>> Select<S>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderByLambda, bool isAsc) { var total = await myDbContext.Set<T>().Where(whereLambda).CountAsync(); if (isAsc) { var entities = await myDbContext.Set<T>().Where(whereLambda) .OrderBy<T, S>(orderByLambda) .Skip(pageSize * (pageIndex - 1)) .Take(pageSize).ToListAsync(); return new Tuple<List<T>, int>(entities, total); } else { var entities = await myDbContext.Set<T>().Where(whereLambda) .OrderByDescending<T, S>(orderByLambda) .Skip(pageSize * (pageIndex - 1)) .Take(pageSize).ToListAsync(); return new Tuple<List<T>, int>(entities, total); } } } |
1 2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class UnitOfWork : IUnitOfWork { private readonly MyDbContext myDbContext; public UnitOfWork(MyDbContext myDbContext) { this.myDbContext = myDbContext; } public MyDbContext GetDbContext() { return myDbContext; } public async Task<int> SaveChangesAsync() { return await myDbContext.SaveChangesAsync(); } } |
1 2 |
1 2 3 4 5 6 |
public class StudentRepository : BaseRepository<Student>, IStudentRepository { public StudentRepository(MyDbContext myDbContext) : base(myDbContext) { } } |
1 2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public interface IBaseService<T> where T : class, new() { Task<int> Insert(T entity); Task<int> Update(T entity); Task<int> Update(Expression<Func<T, bool>> whereLambda, Expression<Func<T, T>> entity); Task<int> Delete(Expression<Func<T, bool>> whereLambda); Task<bool> IsExist(Expression<Func<T, bool>> whereLambda); Task<T> GetEntity(Expression<Func<T, bool>> whereLambda); Task<List<T>> Select(); Task<List<T>> Select(Expression<Func<T, bool>> whereLambda); Task<Tuple<List<T>, int>> Select<S>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderByLambda, bool isAsc); } |
1 2 |
1 2 3 4 |
public interface IStudentService : IBaseService<Student> { Task<bool> UOW(Student student, Teacher teacher); } |
1 2 |
1 2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">StudentService</span> : <span class="hljs-title">BaseService</span><<span class="hljs-title">Student</span>>, <span class="hljs-title">IStudentService</span> { <span class="hljs-keyword">private</span> <span class="hljs-keyword">readonly</span> ITeacherRepository teacherRepository; <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">StudentService</span>(<span class="hljs-params">IUnitOfWork unitOfWork, IBaseRepository<Student> currentRepository, ITeacherRepository teacherRepository</span>) : <span class="hljs-title">base</span>(<span class="hljs-params">unitOfWork, currentRepository</span>)</span> { <span class="hljs-keyword">this</span>.teacherRepository = teacherRepository; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">async</span> Task<<span class="hljs-built_in">bool</span>> <span class="hljs-title">UOW</span>(<span class="hljs-params">Student student, Teacher teacher</span>)</span> { <span class="hljs-keyword">await</span> currentRepository.Insert(student); <span class="hljs-keyword">await</span> teacherRepository.Insert(teacher); <span class="hljs-keyword">await</span> unitOfWork.SaveChangesAsync(); <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
public class BaseService<T> where T : class, new() { protected IUnitOfWork unitOfWork; protected IBaseRepository<T> currentRepository; public BaseService(IUnitOfWork unitOfWork, IBaseRepository<T> currentRepository) { this.unitOfWork = unitOfWork; this.currentRepository = currentRepository; } public async Task<int> Insert(T entity) { await currentRepository.Insert(entity); return await unitOfWork.SaveChangesAsync(); } public async Task<int> Update(T entity) { currentRepository.Update(entity); return await unitOfWork.SaveChangesAsync(); } public async Task<int> Update(Expression<Func<T, bool>> whereLambda, Expression<Func<T, T>> entity) { await currentRepository.Update(whereLambda, entity); return await unitOfWork.SaveChangesAsync(); } public async Task<int> Delete(Expression<Func<T, bool>> whereLambda) { await currentRepository.Delete(whereLambda); return await unitOfWork.SaveChangesAsync(); } public async Task<bool> IsExist(Expression<Func<T, bool>> whereLambda) { return await currentRepository.IsExist(whereLambda); } public async Task<T> GetEntity(Expression<Func<T, bool>> whereLambda) { return await currentRepository.GetEntity(whereLambda); } public async Task<List<T>> Select() { return await currentRepository.Select(); } public async Task<List<T>> Select(Expression<Func<T, bool>> whereLambda) { return await currentRepository.Select(whereLambda); } public async Task<Tuple<List<T>, int>> Select<S>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderByLambda, bool isAsc) { return await currentRepository.Select(pageSize, pageIndex, whereLambda, orderByLambda, isAsc); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
[Route("api/[controller]/[action]")] [ApiController] public class StudentController : ControllerBase { private readonly IStudentService studentService; public StudentController(IStudentService studentService) { this.studentService = studentService; } [HttpPost] public async Task<string> Insert([FromForm] Student student) { try { await studentService.Insert(student); return new Response().ToJson(); } catch (Exception e) { return new Response() { Code = 500, Message = e.Message }.ToJson(); } } [HttpPost] public async Task<string> Update([FromForm] Student student) { try { //await studentService.Update(student); await studentService.Update(t => t.Sid == student.Sid, t => new Student() { Sage = student.Sage }); return new Response().ToJson(); } catch (Exception e) { return new Response() { Code = 500, Message = e.Message }.ToJson(); } } [HttpPost] public async Task<string> Delete(int id) { try { await studentService.Delete(t => t.Sid == id); return new Response().ToJson(); } catch (Exception e) { return new Response() { Code = 500, Message = e.Message }.ToJson(); } } [HttpGet] public async Task<string> Select() { try { var students = await studentService.Select(t => true); return new Response<Student>() { Data = students }.ToJson(); } catch (Exception e) { return new Response() { Code = 500, Message = e.Message }.ToJson(); } } [HttpPost] public async Task<string> UOW([FromForm] Student student) { try { Teacher teacher = new Teacher() { Tid = student.Sid, Tname = student.Sname }; await studentService.UOW(student, teacher); return new Response().ToJson(); } catch (Exception e) { return new Response() { Code = 500, Message = e.Message }.ToJson(); } } } |
1 2 |
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4