ABP集成SqlSugar

SqlSugar是一款老牌 .NET 开源多库架构ORM框架,由果糖大数据科技团队维护和更新 ,生态圈丰富 ,开箱即用最易上手。

在Startup.ConfigureServices中注册SqlSugarClient和SqlSugarRepository

复制代码
//注册上下文:AOP里面可以获取IOC对象,如果有现成框架比如Furion可以不写这一行
services.AddHttpContextAccessor();
//注册SqlSugar
services.AddSingleton<ISqlSugarClient>(s =>
{
	SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
	{
		DbType = SqlSugar.DbType.SqlServer,
		ConnectionString = _appConfiguration.GetConnectionString("Default"),//"Server=.\\SQL16; Database=MaikeABPStudy; Uid=sa; Pwd=123456;",
		IsAutoCloseConnection = true,
	},
   db =>
   {
	   //自动过滤软删除
	   db.QueryFilter.AddTableFilter<ISoftDelete>(it => it.IsDeleted == false);
	   //自动过滤租户
	   db.QueryFilter.AddTableFilter<IMustHaveTenant>(it =>
			it.TenantId == IocManager.Instance.IocContainer.Resolve<IAbpSession>().TenantId);

	   db.Aop.DataExecuting = (_, entityInfo) =>
	   {
		   switch (entityInfo.OperationType)
		   {
			   //执行insert时
			   case DataFilterType.InsertByObject:
				   //自动设置主键: 雪花id,单号
				   if (entityInfo.EntityColumnInfo.IsPrimarykey)
				   {
					   //var obj = (IEntity)entityInfo.EntityValue;
					   //obj.Id = YitIdHelper.NextId();
				   }
				   //自动设置时间和操作人
				   if (entityInfo.EntityValue is ICreationAudited)
				   {
					   var session = IocManager.Instance.IocContainer.Resolve<IAbpSession>();
					   var obj = (ICreationAudited)entityInfo.EntityValue;
					   obj.CreationTime = DateTime.Now;
					   obj.CreatorUserId = session.UserId;
				   }
				   //自动设置租户
				   if (entityInfo.EntityValue is IMustHaveTenant)
				   {
					   var session = IocManager.Instance.IocContainer.Resolve<IAbpSession>();
					   var obj = (IMustHaveTenant)entityInfo.EntityValue;
					   obj.TenantId = session.TenantId.Value;
				   }
				   break;
			   case DataFilterType.UpdateByObject:
				   //自动设置时间和操作人
				   if (entityInfo.EntityValue is IModificationAudited)
				   {
					   var session = IocManager.Instance.IocContainer.Resolve<IAbpSession>();
					   var obj = (IModificationAudited)entityInfo.EntityValue;
					   obj.LastModificationTime = DateTime.Now;
					   obj.LastModifierUserId = session.UserId;
				   }
				   break;
			   case DataFilterType.DeleteByObject:
				   //自动设置时间和操作人
				   if (entityInfo.EntityValue is IDeletionAudited)
				   {
					   var session = IocManager.Instance.IocContainer.Resolve<IAbpSession>();
					   var obj = (IDeletionAudited)entityInfo.EntityValue;
					   obj.DeletionTime = DateTime.Now;
					   obj.DeleterUserId = session.UserId;
				   }
				   break;
		   }
	   };
	   db.Aop.OnLogExecuting = (sql, pars) =>
	   {
		   LogHelper.Logger.Fatal(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, sql, pars));
	   };
	   db.Aop.OnError = ex =>
	   {
		   if (ex.Parametres == null)
		   {
			   LogHelper.Logger.Fatal(ex.Sql);
		   }
		   else
		   {
			   LogHelper.Logger.Fatal(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, ex.Sql, (SugarParameter[])ex.Parametres));
		   }
	   };
   });
	return sqlSugar;
});
//注册SqlSugar仓储
services.AddScoped(typeof(ISqlSugarRepository<>), typeof(SqlSugarRepository<>));

在CORE层添加接口

复制代码
public interface ISqlSugarRepository<T> : ISimpleClient<T> where T : class, new()
{
	ISqlSugarClient Context { get; set; }
    ISugarQueryable<T> Queryable { get; }
}

在EF层添加仓储的实现

复制代码
/// <summary>
/// 基础仓储类
/// </summary>
public class SqlSugarRepository<T> : SimpleClient<T>, ISqlSugarRepository<T> where T : class, new()
{
    public SqlSugarRepository(ISqlSugarClient context = null) : base(context)
    {
    }
	
    public ISugarQueryable<T> Queryable
    {
        get
        {
            return Context.Queryable<T>();
        }
    }
}

在应用层注入使用

复制代码
private ISqlSugarRepository<XXXEntity> _sqlsugarRepository;

//使用
var obj = _sqlsugarRepository.Queryable.First() ;
相关推荐
伍华聪5 天前
在代码生成工具Database2Sharp中对Vue3+ElementPlus的BS端和Winform端界面进行定制生成
sqlsugar·代码生成工具
百锦再7 天前
SQLSugar 封装原理详解:从架构到核心模块的底层实现
sql·mysql·sqlserver·架构·core·sqlsugar·net
伍华聪10 天前
使用HBuilderX把Vue3+Vant4的H5端应用打包为安卓App应用程序
sqlsugar·vue3+typescript
余衫马11 天前
Mysql 5.7 与 SqlSugar 5.X 整合开发实战
mysql·c#·orm·sqlsugar
伍华聪17 天前
在Vant4+Vue3+TypeScript的H5移动前端使用UnoCSS
sqlsugar·vue3+typescript
伍华聪17 天前
在Vue3+ElementPlus前端中,使用watch监控对象变化,实现字典列表的级联更新处理
sqlsugar·vue3+typescript
伍华聪1 个月前
在Vue3+ElementPlus前端中增加对@wangeditor的富文本编辑器和上传文件的处理的封装,实现系统新闻资讯的管理
sqlsugar
伍华聪1 个月前
在Vue3+ElementPlus前端中增加表格记录选择的自定义组件,通过结合Popover 弹出框和Input输入框或者按钮选择实现
sqlsugar·vue3+typescript
伍华聪1 个月前
在SqlSugar的开发框架的Vue3+ElementPlus前端中增加对报表模块的封装处理,实现常规报表的快速处理
sqlsugar
百锦再3 个月前
.Net 优秀框架 ABP全面详解
microsoft·.net·web·blazor·abp·razor