SqlSugar5.0定義倉儲模式


倉儲說明

倉儲可以讓你的方法更加的規范,需要什么方法都封裝到倉儲中,下次就能重復使用,並且能很好的和你業務拆分開

這種設計模式簡單粗暴用起來也方便

引用:

創建倉儲

定義 After.Repository 類庫

定義的Repository是公用類

using After.Generic;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace After.Repository.Repository
{
    public class Repository<T> : SimpleClient<T> where T : class, new()
    {
        public Repository(ISqlSugarClient context = null) : base(context)//注意這里要有默認值等於null
        {
            if (context == null)
            {
                string[] sqlText = Type2.SqlText();
                base.Context = new SqlSugarClient(new ConnectionConfig()
                {
                    //DbType = SqlSugar.DbType.SqlServer,
                    DbType = (DbType)Convert.ToSByte(sqlText[1]),
                    InitKeyType = InitKeyType.Attribute,
                    IsAutoCloseConnection = true,
                    ConnectionString = sqlText[0]
                });
            }
        }

        /// <summary>
        /// 擴展方法,自帶方法不能滿足的時候可以添加新方法
        /// </summary>
        /// <returns></returns>
        public List<T> CommQuery(string json)
        {
            //base.Context.Queryable<T>().ToList();可以拿到SqlSugarClient 做復雜操作
            return null;
        }

    }
}

定義IService Service類庫

IConfigService.cs

using After.Model;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace After.IService.IService
{
    public interface IConfigService
    {
        Task<List<config>> GetAllAsync();
    }
}

ConfigService.cs

using After.IService.IService;
using After.Model;
using After.Repository.Repository;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace After.Service
{
    public class ConfigService : Repository<config>,IConfigService
    {
        public ConfigService()
        {
        }
        /// <summary>
        /// 查詢全部
        /// </summary>
        /// <returns></returns>

        public async Task<List<config>> GetAllAsync()
        {
           return await base.GetListAsync();
        }
    }
}

使用

//創建實例
ConfigService configService = new ConfigService();

	       var date = configService.GetAllAsync();
            foreach (var item in date.Result)
            {
				MessageBox.Show(item.ConfigText);
            }

倉儲自帶方法

//查詢
var data1 = base.GetById(1);//根據id查詢
var data2 = base.GetList();//查詢所有
var data3 = base.GetList(it => it.Id == 1); //根據條件查詢
var data4 = base.GetSingle(it => it.Id == 1);
var p = new PageModel() { PageIndex = 1, PageSize = 2 };
var data5 = base.GetPageList(it => it.Name == "xx", p);
Console.Write(p.PageCount);
var data6 = base.GetPageList(it => it.Name == "xx", p, it => it.Name, OrderByType.Asc);
Console.Write(p.PageCount);
List<IConditionalModel> conModels = new List<IConditionalModel>();
conModels.Add(new ConditionalModel(){FieldName="id",ConditionalType=ConditionalType.Equal,FieldValue="1"});//id=1
var data7 = base.GetPageList(conModels, p, it => it.Name, OrderByType.Asc);
base.AsQueryable().Where(x => x.Id == 1).ToList();
 
//插入
base.Insert(insertObj);
base.InsertRange(InsertObjs);
var id = base.InsertReturnIdentity(insertObj);
base.AsInsertable(insertObj).ExecuteCommand();
 
 
//刪除
base.Delete(insertObj);
base.DeleteById(1);
base.DeleteByIds(new object [] { 1, 2 }); //數組帶是 ids方法 ,封裝傳 object [] 類型
base.Delete(it => it.Id == 1);
base.AsDeleteable().Where(it => it.Id == 1).ExecuteCommand();
 
//更新
base.Update(insertObj); 
base.UpdateRange(InsertObjs); 
base.Update(it => new Order() { Name = "a", }, it => it.Id == 1);
base.AsUpdateable(insertObj).UpdateColumns(it=>new { it.Name }).ExecuteCommand();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM