倉儲說明
倉儲可以讓你的方法更加的規范,需要什么方法都封裝到倉儲中,下次就能重復使用,並且能很好的和你業務拆分開
這種設計模式簡單粗暴用起來也方便
創建倉儲
定義 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();