(4)仓库操作类方法的实现

(4)仓库操作类方法的实现_第1张图片
(4)仓库操作类方法的实现_第2张图片
(4)仓库操作类方法的实现_第3张图片
(4)仓库操作类方法的实现_第4张图片

  • 在CM.Repository下新建上下文类DbContext
    (4)仓库操作类方法的实现_第5张图片
    (4)仓库操作类方法的实现_第6张图片
  • 定义DbContext
    (4)仓库操作类方法的实现_第7张图片
    DbContext.cs
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace CM.Repository
{
    public class DbContext where T : class, new()
    {
        public DbContext()
        {
            var cnn = "server=127.0.0.1;uid=root;pwd=root;database=dotnetcms;sslmode=None";
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = cnn,
                DbType = DbType.MySql,
                IsAutoCloseConnection = true,
            });
            TableClient = new SimpleClient(Db);
        }
        public SqlSugarClient Db { get; private set; }
        public SimpleClient TableClient { get; private set; }
        public static DbContext Instance = new DbContext();
    }
}

  • 之前在 (2)CM.Api项目创建Json包装类与数据库操作接口 中已经创建好了IRepository 基本接口的方法
    (4)仓库操作类方法的实现_第8张图片
  • 创建它的实现类Repository
    (4)仓库操作类方法的实现_第9张图片
    (4)仓库操作类方法的实现_第10张图片
    (4)仓库操作类方法的实现_第11张图片
    Repository.cs
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace CM.Repository
{
    public class Repository : IRepository where T : class, new()
    {
        public DbContext Context = DbContext.Instance;
        public virtual bool Delete(dynamic id)
        {
            return Context.TableClient.Delete(id);
        }

        public bool Delete(Expression> expression)
        {
            return Context.TableClient.Delete(expression);
        }

        public bool DeleteList(List ids)
        {
            return Context.TableClient.DeleteByIds(ids.ToArray());
        }

        public virtual bool DeleteList(string ids)
        {
            var list = ids.Split('_');
            var idList = new List();
            foreach (string j in list)
            {
                idList.Add(Convert.ToInt64(j));
            }
            return DeleteList(idList);
        }

        public virtual bool Exists(dynamic id)
        {
            return Context.Db.Queryable().InSingle(id) != null;
        }

        public virtual List GetList()
        {
            return Context.TableClient.GetList();
        }

        public List GetList(Expression> expression)
        {
            return Context.Db.Queryable().Where(expression).ToList();
        }

        public List GetList(string where)
        {
            return Context.Db.Queryable().Where(where).ToList();
        }

        public virtual T GetModel(dynamic id)
        {
            return Context.TableClient.GetById(id);
        }

        public virtual T GetModel(Expression> expression)
        {
            T model = null;
            List list = GetList(expression);
            if (list.Count > 0) model = list[0];
            return model;
        }

        public virtual List GetPageList(int pageIndex, int pageSize)
        {
            return Context.Db.Queryable()
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .ToList();
        }

        public List GetPageList(int pageIndex, int pageSize, string where)
        {
            return Context.Db.Queryable().Where(where)
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .ToList();
        }

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

        public bool Insert(T obj)
        {
            return Context.Db.Insertable(obj).ExecuteCommandIdentityIntoEntity();
        }

        public bool Update(T obj)
        {
            return Context.TableClient.Update(obj);
        }
    }
}

你可能感兴趣的:(.NET课程实践)