From 1828117af6e218c1e123a0fb183a371dbee3753c Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 6 May 2019 14:20:43 +0800 Subject: [PATCH] Update Demo --- .../Demo/Demo1_SqlSugarClient.cs | 83 +++++- Src/Asp.Net/SqlSugar/SimpleClient.cs | 250 +++++++++--------- 2 files changed, 214 insertions(+), 119 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs b/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs index a0f120b3b..ec78d4b08 100644 --- a/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs +++ b/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs @@ -13,11 +13,66 @@ namespace OrmTest public static void Init() { + SqlSugarClient(); + DbContext();//使用DbContext美化代码 SingletonPattern();//单例 DistributedTransactionExample();//分布式事务 CustomAttribute();//自定义特性 } + private static void SqlSugarClient() + { + + } + + private static void DbContext() + { + + var insertObj = new Order { Name = "jack", CreateTime=DateTime.Now }; + var InsertObjs = new Order[] { insertObj }; + + DbContext context = new DbContext(); + + context.Db.CodeFirst.InitTables();//Create Tables + ; + var orderDb = context.OrderDb; + + //Select + var data1 = orderDb.GetById(1); + var data2 = orderDb.GetList(); + var data3 = orderDb.GetList(it => it.Id == 1); + var data4 = orderDb.GetSingle(it => it.Id == 1); + var p = new PageModel() { PageIndex = 1, PageSize = 2 }; + var data5 = orderDb.GetPageList(it => it.Name == "xx", p); + Console.Write(p.PageCount); + var data6 = orderDb.GetPageList(it => it.Name == "xx", p, it => it.Name, OrderByType.Asc); + Console.Write(p.PageCount); + List conModels = new List(); + conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" });//id=1 + var data7 = orderDb.GetPageList(conModels, p, it => it.Name, OrderByType.Asc); + orderDb.AsQueryable().Where(x => x.Id == 1).ToList(); + + //Insert + orderDb.Insert(insertObj); + orderDb.InsertRange(InsertObjs); + var id = orderDb.InsertReturnIdentity(insertObj); + orderDb.AsInsertable(insertObj).ExecuteCommand(); + + + //Delete + orderDb.Delete(insertObj); + orderDb.DeleteById(1); + orderDb.DeleteById(new int[] { 1, 2 }); + orderDb.Delete(it => it.Id == 1); + orderDb.AsDeleteable().Where(it => it.Id == 1).ExecuteCommand(); + + //Update + orderDb.Update(insertObj); + orderDb.UpdateRange(InsertObjs); + orderDb.Update(it => new Order() { Name = "a", }, it => it.Id == 1); + orderDb.AsUpdateable(insertObj).UpdateColumns(it=>new { it.Name }).ExecuteCommand(); + } + private static void CustomAttribute() { Console.WriteLine(""); @@ -91,7 +146,7 @@ namespace OrmTest DbType = DbType.SqlServer, ConnectionString = Config.ConnectionString, InitKeyType = InitKeyType.Attribute, - IsAutoCloseConnection=true, + IsAutoCloseConnection = true, AopEvents = new AopEvents() { OnLogExecuting = (sql, p) => { Console.WriteLine(sql); } @@ -164,4 +219,30 @@ namespace OrmTest } + + public class DbContext + { + + public SqlSugarClient Db; + public DbContext() + { + Db = new SqlSugarClient(new ConnectionConfig() + { + ConnectionString = Config.ConnectionString, + DbType = DbType.SqlServer, + IsAutoCloseConnection = true, + InitKeyType = InitKeyType.Attribute, + AopEvents = new AopEvents() + { + OnLogExecuting = (sql, p) => + { + Console.WriteLine(sql); + } + } + }); + } + public SimpleClient OrderDb => new SimpleClient(Db); + public SimpleClient OrderItemDb => new SimpleClient(Db); + } + } diff --git a/Src/Asp.Net/SqlSugar/SimpleClient.cs b/Src/Asp.Net/SqlSugar/SimpleClient.cs index d6bab97b3..6f4618b64 100644 --- a/Src/Asp.Net/SqlSugar/SimpleClient.cs +++ b/Src/Asp.Net/SqlSugar/SimpleClient.cs @@ -6,131 +6,22 @@ using System.Text; namespace SqlSugar { - public partial class SimpleClient - { - protected SqlSugarContext Context { get; set; } - public SqlSugarContext FullClient { get { return this.Context; } } - - private SimpleClient() - { - - } - public SimpleClient(SqlSugarContext context) - { - this.Context = context; - } - - public T GetById(dynamic id) where T : class, new() - { - return Context.Queryable().InSingle(id); - } - public int Count(Expression> whereExpression) - { - return Context.Queryable().Where(whereExpression).Count(); - } - public List GetList() where T : class, new() - { - return Context.Queryable().ToList(); - } - public T GetSingle(Expression> whereExpression) where T : class, new() - { - return Context.Queryable().Single(whereExpression); - } - public List GetList(Expression> whereExpression) where T : class, new() - { - return Context.Queryable().Where(whereExpression).ToList(); - } - public List GetPageList(Expression> whereExpression, PageModel page) where T : class, new() - { - int count = 0; - var result = Context.Queryable().Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count); - page.PageCount = count; - return result; - } - public List GetPageList(Expression> whereExpression, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) where T : class, new() - { - int count = 0; - var result = Context.Queryable().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count); - page.PageCount = count; - return result; - } - public List GetPageList(List conditionalList, PageModel page) where T : class, new() - { - int count = 0; - var result = Context.Queryable().Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count); - page.PageCount = count; - return result; - } - public List GetPageList(List conditionalList, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) where T : class, new() - { - int count = 0; - var result = Context.Queryable().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count); - page.PageCount = count; - return result; - } - public bool IsAny(Expression> whereExpression) where T : class, new() - { - return Context.Queryable().Where(whereExpression).Any(); - } - public bool Insert(T insertObj) where T : class, new() - { - return this.Context.Insertable(insertObj).ExecuteCommand() > 0; - } - public int InsertReturnIdentity(T insertObj) where T : class, new() - { - return this.Context.Insertable(insertObj).ExecuteReturnIdentity(); - } - public bool InsertRange(T[] insertObjs) where T : class, new() - { - return this.Context.Insertable(insertObjs).ExecuteCommand() > 0; - } - public bool InsertRange(List insertObjs) where T : class, new() - { - return this.Context.Insertable(insertObjs).ExecuteCommand() > 0; - } - public bool Update(T updateObj) where T : class, new() - { - return this.Context.Updateable(updateObj).ExecuteCommand() > 0; - } - public bool UpdateRange(T[] updateObjs) where T : class, new() - { - return this.Context.Updateable(updateObjs).ExecuteCommand() > 0; - } - public bool UpdateRange(List updateObjs) where T : class, new() - { - return this.Context.Updateable(updateObjs).ExecuteCommand() > 0; - } - public bool Update(Expression> columns, Expression> whereExpression) where T : class, new() - { - return this.Context.Updateable(columns).Where(whereExpression).ExecuteCommand() > 0; - } - public bool Delete(T deleteObj) where T : class, new() - { - return this.Context.Deleteable().Where(deleteObj).ExecuteCommand() > 0; - } - public bool Delete(Expression> whereExpression) where T : class, new() - { - return this.Context.Deleteable().Where(whereExpression).ExecuteCommand() > 0; - } - public bool DeleteById(dynamic id) where T : class, new() - { - return this.Context.Deleteable().In(id).ExecuteCommand() > 0; - } - public bool DeleteByIds(dynamic[] ids) where T : class, new() - { - return this.Context.Deleteable().In(ids).ExecuteCommand() > 0; - } - } + public partial class SimpleClient where T : class, new() { - protected SqlSugarContext Context { get; set; } - public SqlSugarContext FullClient { get { return this.Context; } } + protected ISqlSugarClient Context { get; set; } + [Obsolete("Use AsSugarClient()")] + public ISqlSugarClient FullClient { get { return this.Context; } } + public ISqlSugarClient AsSugarClient() + { + return this.Context; + } private SimpleClient() { } - public SimpleClient(SqlSugarContext context) + public SimpleClient(ISqlSugarClient context) { this.Context = context; } @@ -272,4 +163,127 @@ namespace SqlSugar return this.Context.Deleteable().In(ids).ExecuteCommand() > 0; } } + + + public partial class SimpleClient + { + protected ISqlSugarClient Context { get; set; } + [Obsolete("Use AsSugarClient()")] + public ISqlSugarClient FullClient { get { return this.Context; } } + public ISqlSugarClient AsSugarClient() + { + return this.Context; + } + + private SimpleClient() + { + + } + public SimpleClient(ISqlSugarClient context) + { + this.Context = context; + } + + public T GetById(dynamic id) where T : class, new() + { + return Context.Queryable().InSingle(id); + } + public int Count(Expression> whereExpression) + { + return Context.Queryable().Where(whereExpression).Count(); + } + public List GetList() where T : class, new() + { + return Context.Queryable().ToList(); + } + public T GetSingle(Expression> whereExpression) where T : class, new() + { + return Context.Queryable().Single(whereExpression); + } + public List GetList(Expression> whereExpression) where T : class, new() + { + return Context.Queryable().Where(whereExpression).ToList(); + } + public List GetPageList(Expression> whereExpression, PageModel page) where T : class, new() + { + int count = 0; + var result = Context.Queryable().Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count); + page.PageCount = count; + return result; + } + public List GetPageList(Expression> whereExpression, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) where T : class, new() + { + int count = 0; + var result = Context.Queryable().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count); + page.PageCount = count; + return result; + } + public List GetPageList(List conditionalList, PageModel page) where T : class, new() + { + int count = 0; + var result = Context.Queryable().Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count); + page.PageCount = count; + return result; + } + public List GetPageList(List conditionalList, PageModel page, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) where T : class, new() + { + int count = 0; + var result = Context.Queryable().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count); + page.PageCount = count; + return result; + } + public bool IsAny(Expression> whereExpression) where T : class, new() + { + return Context.Queryable().Where(whereExpression).Any(); + } + public bool Insert(T insertObj) where T : class, new() + { + return this.Context.Insertable(insertObj).ExecuteCommand() > 0; + } + public int InsertReturnIdentity(T insertObj) where T : class, new() + { + return this.Context.Insertable(insertObj).ExecuteReturnIdentity(); + } + public bool InsertRange(T[] insertObjs) where T : class, new() + { + return this.Context.Insertable(insertObjs).ExecuteCommand() > 0; + } + public bool InsertRange(List insertObjs) where T : class, new() + { + return this.Context.Insertable(insertObjs).ExecuteCommand() > 0; + } + public bool Update(T updateObj) where T : class, new() + { + return this.Context.Updateable(updateObj).ExecuteCommand() > 0; + } + public bool UpdateRange(T[] updateObjs) where T : class, new() + { + return this.Context.Updateable(updateObjs).ExecuteCommand() > 0; + } + public bool UpdateRange(List updateObjs) where T : class, new() + { + return this.Context.Updateable(updateObjs).ExecuteCommand() > 0; + } + public bool Update(Expression> columns, Expression> whereExpression) where T : class, new() + { + return this.Context.Updateable(columns).Where(whereExpression).ExecuteCommand() > 0; + } + public bool Delete(T deleteObj) where T : class, new() + { + return this.Context.Deleteable().Where(deleteObj).ExecuteCommand() > 0; + } + public bool Delete(Expression> whereExpression) where T : class, new() + { + return this.Context.Deleteable().Where(whereExpression).ExecuteCommand() > 0; + } + public bool DeleteById(dynamic id) where T : class, new() + { + return this.Context.Deleteable().In(id).ExecuteCommand() > 0; + } + public bool DeleteByIds(dynamic[] ids) where T : class, new() + { + return this.Context.Deleteable().In(ids).ExecuteCommand() > 0; + } + } + }