1.IRepository如下
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using DealerPlatform.Domain.Models; namespace DealerPlatform.Core.Repository { public interface IRepository<TEntity> where TEntity : BaseEntity { DealerPlatformContext Context { get; } TEntity Add(TEntity entity); Task<TEntity> AddAsync(TEntity entity); TEntity Delete(TEntity entity); TEntity Get(Func<TEntity, bool> predicate); Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate); TEntity GetEntityById(int id); List<TEntity> GetList(); List<TEntity> GetList(Func<TEntity, bool> predicate); Task<List<TEntity>> GetListAsync(); Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate); TEntity Update(TEntity entity); Task<TEntity> UpdateAsync(TEntity entity); } }
2.Repository
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using DealerPlatform.Domain.Models; using Microsoft.EntityFrameworkCore; namespace DealerPlatform.Core.Repository { public class Repository<TEntity> : IRepository<TEntity> where TEntity : BaseEntity { public Repository(DealerPlatformContext context) { Context = context; } public DealerPlatformContext Context { get; } /// <summary> /// 获取集合 /// </summary> /// <returns></returns> public List<TEntity> GetList() { return Context.Set<TEntity>().ToList(); } public async Task<List<TEntity>> GetListAsync() { return await Context.Set<TEntity>().ToListAsync(); } //对集合进行筛选 /// <summary> /// 对集合进行筛选 IEnumerable /// </summary> /// <param name="predicate"></param> /// <returns></returns> public List<TEntity> GetList(Func<TEntity, bool> predicate) { var dbset = Context.Set<TEntity>(); return dbset.Where(predicate).ToList(); } public async Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate) { var dbset = Context.Set<TEntity>(); return await dbset.Where(predicate).ToListAsync(); // return await dbset.WhereAsync(predicate).ToListAsync(); } /// <summary> /// 通过id获取当前实体信息 /// </summary> /// <returns></returns> public TEntity GetEntityById(int id) { var data = Context.Set<TEntity>().Where(s => s.Id == id).FirstOrDefault(); return data; } public TEntity Get(Func<TEntity, bool> predicate) { var dbset = Context.Set<TEntity>(); return dbset.Where(predicate).FirstOrDefault(); } public async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate) { var dbset = Context.Set<TEntity>(); return await dbset.Where(predicate).FirstOrDefaultAsync(); // return await dbset.WhereAsync(predicate).ToListAsync(); } /// <summary> /// 添加方法 /// </summary> /// <param name="entity"></param> /// <returns></returns> public TEntity Add(TEntity entity) { var dbset = Context.Set<TEntity>(); var res = dbset.Add(entity).Entity; Context.SaveChanges(); return res; } public async Task<TEntity> AddAsync(TEntity entity) { var dbset = Context.Set<TEntity>(); var res = (await dbset.AddAsync(entity)).Entity; Context.SaveChanges(); return res; } /// <summary> /// 删除方法 /// </summary> /// <param name="entity"></param> /// <returns></returns> public TEntity Delete(TEntity entity) { var dbset = Context.Set<TEntity>(); var res = dbset.Remove(entity).Entity; Context.SaveChanges(); return res; } /// <summary> /// 修改方法 /// </summary> /// <param name="entity"></param> /// <returns></returns> public TEntity Update(TEntity entity) { var dbset = Context.Set<TEntity>(); var oldData = dbset.Where(s => s.Id == entity.Id).FirstOrDefault(); if (oldData != null) { var updateData = dbset.Update(entity).Entity; Context.SaveChanges(); return updateData; } else { throw new Exception("当前数据异常!"); } } public async Task<TEntity> UpdateAsync(TEntity entity) { var dbset = Context.Set<TEntity>(); var oldData = dbset.Where(s => s.Id == entity.Id).FirstOrDefault(); if (oldData != null) { var updateData = dbset.Update(entity).Entity; Context.SaveChanges(); return updateData; } else { throw new Exception("当前数据异常!"); } } } }
3.在program中注入仓储
builder.Services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
4.linq扩展
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; namespace DealerPlatform.Core.Repository { public static class LinqExtensions { /// <summary> /// 扩展dbset /// </summary> /// <param name="dbset"></param> /// <param name="predicate"></param> /// <typeparam name="TEntity"></typeparam> /// <returns></returns> public async static IAsyncEnumerable<TEntity> WhereAsync<TEntity>(this DbSet<TEntity> dbset, Func<TEntity, bool> predicate) where TEntity : class { var res = dbset.Where(predicate); foreach (var item in res) { yield return item; } } /// <summary> /// 扩展IAsyncEnumerable /// </summary> /// <param name="listAsync"></param> /// <typeparam name="TEntity"></typeparam> /// <returns></returns> public async static Task<List<TEntity>> ToListAsync<TEntity>(this IAsyncEnumerable<TEntity> listAsync) { List<TEntity> list = new(); await foreach (var item in listAsync) { list.Add(item); } return list; } } }
5.接下来就是写我们的具体的业务了,service结构如下,带I的是接口,其余都是class,自行添加类与接口,整体结构如图
注意:Service里除了接口与dto,其余都是部分类partial,不懂得自行百度,用于共享继承接口及属性和字段的
6.CustomerService代码如下
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DealerPlatform.Core.Repository; using DealerPlatform.Domain.Models; namespace DealerPlatform.Service.CustomerApp { /// <summary> /// 部分类实现,共享一些字段,方法及属性,部分类中其中有一个继承接口,其他都继承 /// </summary> public partial class CustomerService : ICustomerService { public CustomerService( IRepository<Customer> customerRepo, IRepository<CustomerInvoice> customerInvoiceRepo, IRepository<CustomerPwd> customerPwdRepo ) { CustomerRepo = customerRepo; CustomerInvoiceRepo = customerInvoiceRepo; CustomerPwdRepo = customerPwdRepo; } public IRepository<Customer> CustomerRepo { get; } public IRepository<CustomerInvoice> CustomerInvoiceRepo { get; } public IRepository<CustomerPwd> CustomerPwdRepo { get; } } }
7.Customer.Invoice暂时没有代码,Customer.Pwd代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DealerPlatform.Core.Repository; using DealerPlatform.Domain.Models; using DealerPlatform.Service.CustomerApp.Dto; namespace DealerPlatform.Service.CustomerApp { public partial class CustomerService { public async Task<bool> CheckPassword(CustomerLoginDto dto) { // if (string.IsNullOrWhiteSpace(dto.CustomerNo) || string.IsNullOrWhiteSpace(dto.Password)) // { // throw new Exception("账号或密码不能为空!"); // } //判断当前dto中是否为空 var res = CustomerPwdRepo.GetAsync(s => s.CustomerNo == dto.CustomerNo && s.CustomerPwd1 == dto.Password); if (res != null) { return true; } else { return false; } } } }
8.ICustomerService代码如下:
using DealerPlatform.Core.Repository; using DealerPlatform.Domain.Models; using DealerPlatform.Service.CustomerApp.Dto; namespace DealerPlatform.Service.CustomerApp { public interface ICustomerService { Task<bool> CheckPassword(CustomerLoginDto dto); } }
只是一个登录的逻辑,这里我就不介绍过多了,大家应该都知道的
9.在DealerPlatform.Web的Controllers新增LoginController的api接口
代码如下
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DealerPlatform.Service.CustomerApp; using DealerPlatform.Service.CustomerApp.Dto; using Microsoft.AspNetCore.Mvc; namespace DealerPlatform.Web.Controllers { [Route("[controller]")] public class LoginController : Controller { public LoginController(ICustomerService customerService) { CustomerService = customerService; } public ICustomerService CustomerService { get; } public async Task<string> CheckLogin(CustomerLoginDto dto) { var isSuccess = await CustomerService.CheckPassword(dto); //TODO 获取用户数据 if (isSuccess) { return default; } return default; } } }
10.LoginController想要调用service服务,我们就需要在整个web应用里注入这个服务
注入服务的方法如下:
在program中添加如下代码,在注入控制器之前写这段代码
检索程序集和jwt等,还有密码md5加密会在下面的文章中介绍
builder.Services.AddTransient<ICustomerService, CustomerService>();
11.在appsetting.json中添加如下代码,为jwt做准备
"Jwt": {
"Issuer": "Ace",
"Audience": "Ace",
"Expires": 10,
"Security": "sadsadsadasdasdsadasdasdasdasdasdasdasdasdsad"
}
下一篇主要讲解md5加密及jwt授权鉴权