.NET经销商实战(二)——仓储与服务层完善,及扩展linq
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 where TEntity : BaseEntity
{
DealerPlatformContext Context { get; }
TEntity Add(TEntity entity);
Task AddAsync(TEntity entity);
TEntity Delete(TEntity entity);
TEntity Get(Func predicate);
Task GetAsync(Expression> predicate);
TEntity GetEntityById(int id);
List GetList();
List GetList(Func predicate);
Task> GetListAsync();
Task> GetListAsync(Expression> predicate);
TEntity Update(TEntity entity);
Task 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 : IRepository where TEntity : BaseEntity
{
public Repository(DealerPlatformContext context)
{
Context = context;
}
public DealerPlatformContext Context { get; }
///
/// 获取集合
///
///
public List GetList()
{
return Context.Set().ToList();
}
public async Task> GetListAsync()
{
return await Context.Set().ToListAsync();
}
//对集合进行筛选
///
/// 对集合进行筛选 IEnumerable
///
///
///
public List GetList(Func predicate)
{
var dbset = Context.Set();
return dbset.Where(predicate).ToList();
}
public async Task> GetListAsync(Expression> predicate)
{
var dbset = Context.Set();
return await dbset.Where(predicate).ToListAsync();
// return await dbset.WhereAsync(predicate).ToListAsync();
}
///
/// 通过id获取当前实体信息
///
///
public TEntity GetEntityById(int id)
{
var data = Context.Set().Where(s => s.Id == id).FirstOrDefault();
return data;
}
public TEntity Get(Func predicate)
{
var dbset = Context.Set();
return dbset.Where(predicate).FirstOrDefault();
}
public async Task GetAsync(Expression> predicate)
{
var dbset = Context.Set();
return await dbset.Where(predicate).FirstOrDefaultAsync();
// return await dbset.WhereAsync(predicate).ToListAsync();
}
///
/// 添加方法
///
///
///
public TEntity Add(TEntity entity)
{
var dbset = Context.Set();
var res = dbset.Add(entity).Entity;
Context.SaveChanges();
return res;
}
public async Task AddAsync(TEntity entity)
{
var dbset = Context.Set();
var res = (await dbset.AddAsync(entity)).Entity;
Context.SaveChanges();
return res;
}
///
/// 删除方法
///
///
///
public TEntity Delete(TEntity entity)
{
var dbset = Context.Set();
var res = dbset.Remove(entity).Entity;
Context.SaveChanges();
return res;
}
///
/// 修改方法
///
///
///
public TEntity Update(TEntity entity)
{
var dbset = Context.Set();
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 UpdateAsync(TEntity entity)
{
var dbset = Context.Set();
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
{
///
/// 扩展dbset
///
///
///
///
///
public async static IAsyncEnumerable WhereAsync(this DbSet dbset, Func predicate)
where TEntity : class
{
var res = dbset.Where(predicate);
foreach (var item in res)
{
yield return item;
}
}
///
/// 扩展IAsyncEnumerable
///
///
///
///
public async static Task> ToListAsync(this IAsyncEnumerable listAsync)
{
List 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
{
///
/// 部分类实现,共享一些字段,方法及属性,部分类中其中有一个继承接口,其他都继承
///
public partial class CustomerService : ICustomerService
{
public CustomerService(
IRepository customerRepo,
IRepository customerInvoiceRepo,
IRepository customerPwdRepo
)
{
CustomerRepo = customerRepo;
CustomerInvoiceRepo = customerInvoiceRepo;
CustomerPwdRepo = customerPwdRepo;
}
public IRepository CustomerRepo { get; }
public IRepository CustomerInvoiceRepo { get; }
public IRepository 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 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 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 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();
11.在appsetting.json中添加如下代码,为jwt做准备
"Jwt": {
"Issuer": "Ace",
"Audience": "Ace",
"Expires": 10,
"Security": "sadsadsadasdasdsadasdasdasdasdasdasdasdasdsad"
}
下一篇主要讲解md5加密及jwt授权鉴权