从零开始搭建自己的网站四:后端常用插件使用介绍

1.jwt

网上很多封装教程,

简单来说就是创建一个信息载荷(SecurityToken)然后SecurityTokenHandler.WriteToken()创建一个token

附上MS官网文档

SecurityTokenHandler.WriteToken Method (System.IdentityModel.Tokens) | Microsoft Learn

2.sqlsugar

简单封装,创建一个类继承

public class Repository<T> : SimpleClient<T> where T : class, new()
{
    public Repository(ISqlSugarClient db)
    {          
       base.Context=db; 
    }
 
    /// <summary>
    /// 扩展方法,自带方法不能满足的时候可以添加新方法
    /// </summary>
    /// <returns></returns>
    public List<T> CommQuery(string json)
    {
       //base.Context.Queryable<T>().ToList();可以拿到SqlSugarClient 做复杂操作
       return null;
    }
     
}

然后注入.net服务

services.AddScoped<ISqlSugarClient>(s =>
{
        //Scoped用SqlSugarClient 
    SqlSugarClient sqlSugar = new SqlSugarClient (new ConnectionConfig()
    {
        DbType = SqlSugar.DbType.Sqlite,
        ConnectionString = "DataSource=sqlsugar-dev.db",
        IsAutoCloseConnection = true,
    },
   db =>
   {
       //单例参数配置,所有上下文生效
       db.Aop.OnLogExecuting = (sql, pars) =>
       {
           //获取IOC对象不要求在一个上下文
           //vra log=s.GetService<Log>()
                
           //获取IOC对象要求在一个上下文
           //var appServive = s.GetService<IHttpContextAccessor>();
           //var log= appServive?.HttpContext?.RequestServices.GetService<Log>();
       };
   });
    return sqlSugar;
});

附上官网地址

仓储定义 - SqlSugar 5x - .NET果糖网 (donet5.com)

3.autoMapper

继承 Profile类,写入mapping配置关系

public class OrganizationProfile : Profile
{
	public OrganizationProfile()
	{
		CreateMap<Foo, FooDto>();
		// Use CreateMap... Etc.. here (Profile methods are the same as configuration methods)
	}
}

注入service

services.AddAutoMapper(typeof(OrganizationProfile), typeof(ProfileTypeFromAssembly2) /*, ...*/);

附上官网地址

Configuration — AutoMapper documentation

4.自动依赖注入

原理就是创建一个实例,反射获取其子类和接口,自动注入进service

5.filter

继承AuthorizeFilter,重写OnAuthorization方法,从方法参数context中获取token然后解析

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值