.net core 2.2webapi+SqlSugar ORM搭建基本的框架

第一步 创建项目,并添加相关的组件包

创建.net core的项目过程和添加引用包这里我就不写出来了,自行解决。

第二步 创建数据层

1创建数据连接

.net core 2.2webapi+SqlSugar ORM搭建基本的框架_第1张图片

第三步 创建仓储,为了便于以后的扩展建议多建一个类库用作自定义仓储,我这里快速就创建在数据层了

.net core 2.2webapi+SqlSugar ORM搭建基本的框架_第2张图片

1定义仓储接口 接口定义未常用的操作方法就行了

.net core 2.2webapi+SqlSugar ORM搭建基本的框架_第3张图片

2实现仓储业务

  ///


    /// 仓储实现层
    ///

    ///
    public class Repository : DbSqlContext,IRepository where T : class, new()
    {
        ///
        /// 构造参数
        ///

        ///
       public Repository(IConfiguration configure):base(configure)
       {

       }
        ///


        /// 添加数据
        ///

        ///
        ///
        public int Add(T model)
        {
            return Db.Insertable(model).ExecuteCommand();
        }
        ///
        /// 添加数据并返回主键
        ///

        ///
        ///
        public int RetuIdAdd(T model)
        {
            return Db.Insertable(model).ExecuteReturnIdentity();
        }
        ///
        /// 添加数据并返回插入的实体
        ///

        ///
        ///
        public T RetuEntityAdd(T model)
        {
            return Db.Insertable(model).ExecuteReturnEntity();
        }
        ///
        /// 根据条件查询对象
        ///

        ///
        ///

        public T DataItem(Expression> WhereLambda)
        {
            return Db.Queryable().Where(WhereLambda).Single();
        }
        ///


        /// 根据条件分页查询列表
        ///

        ///
        ///
        ///
        ///
        public List DataPages(Expression> WhereLambda,int pageIndex,int pageSize)
        {
            return Db.Queryable().Where(WhereLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }

        ///


        /// 根据条件查询列表
        ///

        ///
        ///
        public List GetList(Expression> WhereLambda)
        {

            return Db.Queryable().Where(WhereLambda).ToList();
        }
        ///


        /// 查询所有数据
        ///

        ///
        public List GetList()
        {

            return Db.Queryable().ToList();
        }

        ///


        /// 删除数据
        ///

        ///
        ///
        public int Delete(Expression> WhereLambda)
        {
            return Db.Deleteable(WhereLambda).ExecuteCommand();
        }
        
        ///
        /// 更新数据
        ///

        ///
        ///
        public int Update(T model)
        {
            return Db.Updateable(model).ExecuteCommand();
        }
        public int Update(List list)
        {
            return Db.Updateable(list).ExecuteCommand();
        }
        ///
        /// 查询总记录数
        ///

        ///
        ///
        public int DataCount(Expression> WhereLambda)
        {
            return Db.Queryable().ToList().Count;
        }
        ///
        /// 查询总记录数
        ///

        ///
        ///
        public int DataCount(List conModels)
        {
            return Db.Queryable().Where(conModels).Count();
        }
        ///
        /// 批量删除
        ///

        ///
        ///
        public int Delete(dynamic[] ids)
        {
            return Db.Deleteable(ids).ExecuteCommand();
        }
    }

第四步 关键点 在web层的Startup注入仓储和业务层

//业务层注入
            Assembly assemblys = Assembly.Load("QuestionnaireSurvey.BLL");
            Assembly assemblysInterFace = Assembly.Load("QuestionnaireSurvey.IBLL");
            var typesInterface = assemblysInterFace.GetTypes();
            var typesImpl = assemblys.GetTypes();
           
            foreach (var item in typesInterface)
            {
                var name = item.Name.Substring(1);
                string implBLLImpName = name + "Server";
                var impl = typesImpl.FirstOrDefault(w => w.Name.Equals(implBLLImpName));

                if (impl != null)
                {
                    services.AddTransient(item, impl);
                }
            }
            //注入数据层
            services.AddTransient(typeof(DbSqlContext));
            //注入仓储
            services.AddTransient(typeof(IRepository<>), typeof(Repository<>));

第五步  调用

 ///


    /// 业务层 处理业务
    ///

    public class ArtcleBLLServer : IArtcleBLL
    {
        ///
        /// 声明要调用的仓储
        ///

        public IRepository _respond;
        ///
        /// 构造参数
        ///

        ///
        public ArtcleBLLServer(IRepository respond)
        {
            _respond = respond;
        }
        ///
        /// 查询所有数据
        ///

        ///
        public List GetAll()
        {
            return _respond.GetList();
        }
    }

简单的.net core api+仓储+SqlSugar MOR框架就搭建好了

你可能感兴趣的:(框架,系统架构,.net)